Url paramter:
Copy to ClipboardReference: [www.bkjia.com] // lastest:
Var getArgs = function () {// get url querystring
Var params = document. location. search, reg = /(? : ^ \? | &)(.*?) = (.*?) (? = & | $)/G, temp, args = {};
While((temp1_reg.exe c (params ))! = Null) args [temp [1] = decodeURIComponent (temp [2]);
Return args;
};
// Get 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 to ClipboardReference: [www.bkjia.com] var getScriptArgs = function () {// obtain multiple parameters
Var scripts = document. getElementsByTagName ("script "),
Script = scripts [scripts. length-1], // because the script label after the current dom is loaded is not yet loaded, the last one is the current script
Src = script. src, title = "bkjia.com"
Reg = /(? :\? | &)(.*?) = (.*?) (? = & | $)/G,
Temp, res = {};
While((temp1_reg.exe c (src ))! = Null) res [temp [1] = decodeURIComponent (temp [2]);
Return res;
};
Var args = getScriptArgs ();
Alert (args. a + "|" + args. B + "|" + args. c );
// Assume that the above js is in the js1.js script <script type = "text/javascript" src = "js1.js? A = abc & B = Chinese Characters & c = 123 "> </script>
Var getScriptArg = function (key) {// obtain 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. Otherwise, the parameters of the last js file may always be obtained. To use the parameters in the method, save them with variables and obtain them directly in the method.