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