Methods for extracting URL parameters in JS
First, find the value on the right of the equal sign of the parameter in the regular expression. However, if this parameter is not followed by &, it is not feasible.
<Script language = "JavaScript">
VaR STR = Window. Location. href;
VaR es =/clid = /;
Es.exe C (STR );
VaR right = Regexp. rightcontext;
If (Right = "1 ")
{
Sub_nav_4.style.display = "Block ";
Li4.style. Background = "# a1ca00 ";
}
</SCRIPT> second, you can pay all the parameters you can find to the argsarr array. In the future, you can use argsarr [I] for access. The method is good, too long, and the parameters are hard to remember. /**//**
* Extract parameters in a URL
*/
Function getargs ()
{
// Add the substring to remove the question mark (?) in the query string? .
// Var query = Window. Location. Search. substring (1 );
// Define an array to store the obtained string parameters.
VaR argsarr = new object ();
// Obtain the query string parameters in the URL
VaR query = Window. Location. search;
Query = query. substring (1 );
// Here the pairs is a String Array
VaR pairs = query. Split ("&"); // name = myname & Password = 1234 & sex = male & Address = Nanjing
For (VAR I = 0; I <pairs. length; I ++)
{
VaR Sign = pairs [I]. indexof ("= ");
// If no = is found, skip and jump to the next string (next loop ).
If (Sign =-1)
{
Continue;
}
VaR akey = pairs [I]. substring (0, sign );
VaR avalue = pairs [I]. substring (sign + 1 );
Argsarr [akey] = avalue;
}
Return argsarr;
}
Third, the simplest method is similar to the second method. However, you can directly access the parameter name without creating an array storage parameter, which is easy to understand. <Script language = "JavaScript">
Function getarg ()
{
VaR url = Unescape (window. Location. href );
VaR allargs = URL. Split ("? ") [1];
VaR ARGs = allargs. Split ("&");
For (VAR I = 0; I <args. length; I ++)
{
VaR Arg = ARGs [I]. Split ("= ");
Eval ('this. '+ Arg [0] +' = "'+ Arg [1] + '";');
}
}
VaR urlarg = new getarg ();
If (urlarg. clid = "1 ")
{
Sub_nav_6.style.display = "Block ";
Li6.style. Background = "# a1ca00 ";
}
</SCRIPT>