JavaScript gets URL parameters and script tags to get URL parameter function code _javascript tips

Source: Internet
Author: User
Tags script tag
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
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.