今天在写BLOG的Trackback时,需要用到当前页的URL地址,并且包括?后的所有参数。在网上看到以下的这段ASP代码,它的确可以获取到当前页的URL,包括参数。
'============================================
'获取当前Url参数的函数
Function GetUrl()
Dim ScriptAddress, M_ItemUrl, M_item
ScriptAddress = CStr(Request.ServerVariables("SCRIPT_NAME"))
M_ItemUrl = ""
If (Request.QueryString <> "") Then
ScriptAddress = ScriptAddress & "?"
For Each M_item In Request.QueryString
If InStr(page,M_Item)=0 Then
M_ItemUrl = M_ItemUrl & M_Item &"="& Server.URLEncode(Request.QueryString(""&M_Item&"")) & "&"
End If
Next
end if
GetUrl ="http://"&Request.ServerVariables("Server_Name")&ScriptAddress & M_ItemUrl
End Function
'==============================================
'获取当前Url参数的函数
Function GetUrl()
Dim ScriptAddress, M_ItemUrl, M_item
ScriptAddress = CStr(Request.ServerVariables("SCRIPT_NAME"))
M_ItemUrl = ""
If (Request.QueryString <> "") Then
ScriptAddress = ScriptAddress & "?"
For Each M_item In Request.QueryString
If InStr(page,M_Item)=0 Then
M_ItemUrl = M_ItemUrl & M_Item &"="& Server.URLEncode(Request.QueryString(""&M_Item&"")) & "&"
End If
Next
end if
GetUrl ="http://"&Request.ServerVariables("Server_Name")&ScriptAddress & M_ItemUrl
End Function
'==============================================
但这段ASP代码有不足,比如:http://www.cn-pn.com/diary/show.asp?id=853,获取的地址却是http://www.cn-pn.com/diary/show.asp?id=853&,地址后多了个“&”,这是由于它的循环没有处理好。
其实根本没那么费事;以下是我自己加工的ASP代码,短小且功能正常,有需要的朋友就拿去用吧:
'===========================================
'获取当前Url参数的函数
Function GetUrl()
Dim ScriptAddress,Servername,qs
ScriptAddress = CStr(Request.ServerVariables("SCRIPT_NAME"))
Servername = CStr(Request.ServerVariables("Server_Name"))
qs=Request.QueryString
if qs<>"" then
GetUrl ="http://"& Servername & ScriptAddress &"?"&qs
else
GetUrl ="http://"& Servername & ScriptAddress
end if
End Function
'============================================
'获取当前Url参数的函数
Function GetUrl()
Dim ScriptAddress,Servername,qs
ScriptAddress = CStr(Request.ServerVariables("SCRIPT_NAME"))
Servername = CStr(Request.ServerVariables("Server_Name"))
qs=Request.QueryString
if qs<>"" then
GetUrl ="http://"& Servername & ScriptAddress &"?"&qs
else
GetUrl ="http://"& Servername & ScriptAddress
end if
End Function
'============================================
全站熱搜
留言列表