링크 매개변수 가져오기

4948 단어
1、url paramter
//
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; 
}; 
//
var queryString=function(key){ 
    return (document.location.search.match(new RegExp("(?:^\\?|&)"+key+"=(.*?)(?=&|$)"))||['',null])[1]; 
} 

2、script paramter
//  
var getScriptArgs=function(){
    var scripts=document.getElementsByTagName("script"), 
    script=scripts[scripts.length-1],// dom script , 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); 
//<script type="text/javascript" src="js1.js?a=abc&b= &c=123"></script> 

//  
var getScriptArg=function(key){
    var scripts=document.getElementsByTagName("script"), 
    script=scripts[scripts.length-1], 
    src=script.src; 
    return (src.match(new RegExp("(?:\\?|&)"+key+"=(.*?)(?=&|$)"))||['',null])[1]; 
}; 
alert(getScriptArg("c"));

좋은 웹페이지 즐겨찾기