URL paramter:
Copy Code code as follows:
Lastest:
var getargs=function () {//get URL querystring
var params=document.location.search,reg=/(?: ^\?| &) (. *?) =(.*?) (? =&|$)/g,temp,args={};
while ((Temp=reg.exec (params))!=null) args[temp[1]]=decodeuricomponent (temp[2));
return args;
};
Take only one:
var querystring=function (key) {
Return (Document.location.search.match new RegExp (?: ^\ \?| &) "+key+" = (. *?) (? =&|$)) | | [", NULL]) [1];
}
var Args=getargs ();
Alert (args.name+) | "+args.sex+" | "+args.age);
Test Link: <a href= "? name=abc&sex= male &age=12" >test getquerystring</a>
Script paramter:
Copy Code code as follows:
var getscriptargs=function () {//Get multiple parameters
var scripts=document.getelementsbytagname ("script"),
script=scripts[scripts.length-1],//because the script tag that follows the current DOM load is not loaded, the last one is the current script
SRC=SCRIPT.SRC,
reg=/(?: \?| &) (. *?) =(.*?) (? =&|$)/g,
temp,res={};
while ((Temp=reg.exec (SRC))!=null) res[temp[1]]=decodeuricomponent (temp[2));
return res;
};
var Args=getscriptargs ();
Alert (args.a+) | "+args.b+" | "+ARGS.C);
If the above JS is in this js1.js script <script type= "Text/javascript" src= "js1.js?a=abc&b= Chinese characters &c=123" ></script >
var getscriptarg=function (key) {//Get a single parameter
var scripts=document.getelementsbytagname ("script"),
Script=scripts[scripts.length-1],
SRC=SCRIPT.SRC;
Return (Src.match new RegExp (?: \ \?| &) "+key+" = (. *?) (? =&|$)) | | [", NULL]) [1];
};
Alert (Getscriptarg ("C"));
PS: Do not call the method in the method, or you may always get the last JS file parameters, to be used in the method, please first save the variable, in the method directly to obtain