在添加內容過程中,我們經常需要添加一些網址,同時需要這些網址顯示出來就可以點擊并在新窗口打開.例如客戶信息備注,訂單信息備注等內容中經常要使用到此類功能,下面由南昌網站建設公司工程向大家介紹實現方法,以下代碼也是經過常期使用,確定功能達到預期要求,希望此代碼能對廣大南昌網絡公司技術人員有所幫助:
1.例如:有以下內容
南昌網站設計公司網址是http://www.527701.com 其中有一篇專業介紹Mysql中DATE_SUB 使用方法結合查詢一天內,一周內,一月內的文章 http://www.527701.com/newshow.asp?id=1347&tag=2
我們在文本框內輸入這些內容,前臺顯示出來就能打開這些鏈接.
2.定義變量(也可以獲取數據庫內容)
Content="南昌網站設計公司網址是http://www.527701.com/ 其中有一篇專業介紹Mysql中DATE_SUB 使用方法結合查詢一天內,一周內,一月內的文章?http://www.527701.com/newshow.asp?id=1347&tag=2
3.輸出?
response.Write(AutoLink(Content))
重點:
Function AutoLink(TextContent)
Dim match, matches, offset, url, email, link, relnkAutoLinkURL
relnkAutoLinkURL = "[[%URLText%]]"
If Not IsObject(regExp) Then Set regExp = New RegExp
regExp.Global = True
regExp.IgnoreCase = True
'查找鏈接地址
regExp.Pattern = "(((ht|f)tps?://)|(www.))([w-]+.)+[w-:]+(/[w- ./?%#;&=]*)?"
Set matches = regExp.Execute(TextContent)
offset = 0
For Each match in matches
url = match
If Left(url, 4) = "www." Then url = "http://" & url
link = Replace(Replace(relnkAutoLinkURL, "[[%URLText%]]", match), "[[%URL%]]", url)
TextContent = Mid(TextContent, 1, match.FirstIndex + offset) & link & Mid(TextContent, match.FirstIndex + 1 + match.Length + offset, Len(TextContent))
offset = offset + Len(link) - Len(match)
Next
'查找郵箱地址
regExp.Pattern = "[A-Za-z0-9_+-.']+@w+([-.]w+)*.w+([-.]w+)*"
Set matches = regExp.Execute(TextContent)
offset = 0
For Each match in matches
email = match
link = Replace(Replace(relnkAutoLinkURL, "[[%URLText%]]", match), "[[%URL%]]", "mailto:" & email)
TextContent = Mid(TextContent, 1, match.FirstIndex + offset) & link & Mid(TextContent, match.FirstIndex + 1 + match.Length + offset, Len(TextContent))
offset = offset + Len(link) - Len(match)
Next
AutoLink = TextContent
End Function
可以根據用戶需求可控制是否在新窗口打開鏈接,還是在當前窗口打開鏈接,由此處代碼控制,[[%URLText%]],如果不加 target='_blank' 則在當前窗口打開該鏈接.