JavaScript gets URL parameters and parameter values

Source: Internet
Author: User

<! 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.