<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>javascript get URL parameters and parameter values </title>
<script type= "Text/javascript" >
<!--
var url = "http://www.baidu.com/?age=25&k=1&site=asp&abc=123;"
JavaScript gets the specified parameter and its corresponding value
function getparameter (parastr, URL)
{
var result = "";
Get all parameter list data in a URL
var str = "&" + Url.split ("?") [1];
var paraname = parastr + "=";
Determine if the parameter to get is present
if (Str.indexof ("&" +paraname)!=-1)
{
If you want to get the argument to the end also contains "&"
if (Str.substring (Str.indexof (paraname), str.length). IndexOf ("&")!=-1)
{
Gets the argument to the end of the string
var tmpstr=str.substring (Str.indexof (paraname), str.length);
Intercepts characters from the beginning of the argument to the nearest "&" Occurrence
Result=tmpstr.substr (Tmpstr.indexof (Paraname), Tmpstr.indexof ("&")-tmpstr.indexof (Paraname));
}
Else
{
Result=str.substring (Str.indexof (paraname), str.length);
}
}
Else
{
result= "without this parameter";
}
Return (Result.replace ("&", ""));
}
Calling method: var variable name = getparameter ("parameter name to get", url address)
var r = getparameter ("Age", url);
Test output, the result is: site=popasp
Alert (R);
Depending on the results obtained, you can use
var pName = r.split ("=") [0]; Get parameter name
var pValue = r.split ("=") [1]; Get parameter values
Test output:
Alert ("Parameter name:" + pName + "\ n" + "parameter value:" + pValue);
Other practical applications:
According to the need, with the following methods to achieve their own functions to achieve;
var hostname = Location.hostname; Gets the current domain name (does not contain HTTP//)
var localurl = location.href; Gets the current full URL address information (contains HTTP//, domain name, path, specific file, and all pass parameters)
var referurl = Document.referrer; Gets the full URL information for the previous page (contains HTTP//, domain name, path, specific file, and all pass parameters)
-
</script>
<body>
</body>
JavaScript gets URL parameters and parameter values