Sample Code for javascript to obtain URL parameters and parameter values _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces the sample code for javascript to obtain URL parameters and parameter values. For more information, see. The Code is 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 retrieves the specified parameter and its corresponding value
Function getParameter (paraStr, url)
{
Var result = "";
// Obtain all parameter list data in the URL
Var str = "&" + url. split ("? ") [1];
Var paraName = paraStr + "= ";
// Determine whether the parameter to be obtained exists
If (str. indexOf ("&" + paraName )! =-1)
{
// If the parameters to be obtained still contain "&" at the end
If (str. substring (str. indexOf (paraName), str. length). indexOf ("&")! =-1)
{
// Obtain the string from the end of the parameter to be obtained
Var TmpStr = str. substring (str. indexOf (paraName), str. length );
// Intercept the characters from the beginning of the parameter to the nearest "&" position
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 complete and tested.

The Code is as follows:






Javascript to obtain 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.