JavaScript gets the sample code for URL parameters and parameter values _javascript tips

Source: Internet
Author: User
Copy Code code as follows:

<script>
function Goto_url (URL) {
var new_url = "Http://shop.usteel.com/index.php?app=list_release";
var d_date = getparameter ("date", url);
if (d_date!= ') {
New_url + = "&" +d_date;
}
var species = getparameter ("species", url);
If (species!= ') {
New_url + = "&" +species;
}

window.open (New_url);

}
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 + "=";
Determines whether the parameter to be obtained exists
if (Str.indexof ("&" +paraname)!=-1)
{
If you want to get a parameter to the end that also contains "&"
if (str.substring str.indexof (paraname), str.length). IndexOf ("&")!=-1)
{
Get the argument to get to the end of the string
var tmpstr=str.substring (Str.indexof (paraname), str.length);
Intercepts the characters between the start of a parameter and the location of the nearest "&"
Result=tmpstr.substr (Tmpstr.indexof (Paraname), Tmpstr.indexof ("&")-tmpstr.indexof (Paraname));
}
Else
{
Result=str.substring (Str.indexof (paraname), str.length);
}
}
Else
{
Result= "";
}
Return (Result.replace ("&", ""));
}
</script>

The following example is a complete test
Copy Code code as follows:

<! 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 + "=";
Determines whether the parameter to be obtained exists
if (Str.indexof ("&" +paraname)!=-1)
{
If you want to get a parameter to the end that also contains "&"
if (str.substring str.indexof (paraname), str.length). IndexOf ("&")!=-1)
{
Get the argument to get to the end of the string
var tmpstr=str.substring (Str.indexof (paraname), str.length);
Intercepts the characters between the start of a parameter and the location of the nearest "&"
Result=tmpstr.substr (Tmpstr.indexof (Paraname), Tmpstr.indexof ("&")-tmpstr.indexof (Paraname));
}
Else
{
Result=str.substring (Str.indexof (paraname), str.length);
}
}
Else
{
result= "no such parameter";
}
Return (Result.replace ("&", ""));
}

Call 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]; Gets the name of the parameter
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;
var hostname = Location.hostname; Get current domain name (does not contain http://)
var localurl = location.href; Get the current complete URL address information (including http://, domain name, path, specific file and all pass parameters)
var referurl = Document.referrer; Get the complete URL information on the previous page (including http://, domain name, path, specific file and all pass parameters)

-->
</script>

<body>
</body>

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.