JS extract URL parameters of several methods. (collection) __js
Source: Internet
Author: User
First, the regular expression, directly find the value of the right side of the parameter equals. However, it is not feasible to have & after this parameter.
< SCRIPT language = "JavaScript" >
var str = window.location.href;
var es =/clid =/;
Es.exec (str);
var right = Regexp.rightcontext;
if (right = = "1")
{
Sub_nav_4.style.display = "block";
Li4.style.background = "#a1ca00";
}
</script > Second, the parameters can be found are paid to Argsarr array, in the future can be used Argsarr[i] for corresponding access, the method is good, too long, parameters are not easy to remember. /**
* Extract the parameters from the URL
*/
function Getargs ()
{
Plus the meaning of substring is to get rid of the query string. Resolution
var query = window.location.search.substring (1);
Defines an array that holds the string arguments that are taken out.
var Argsarr = new Object ();
Gets the query string parameter in the URL
var query = Window.location.search;
query = query.substring (1);
The pairs here is an array of strings
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 = number is found, skip and skip 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, similar to the second, but did not create an array of stored parameters, you can directly access the name of the parameter, easy to read. < SCRIPT language = "JavaScript" >
function Getarg ()
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.