Get url parameters in JS and get url parameters in script tags

Source: Internet
Author: User

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.

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.