Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

获取给js文件传的参数 #16

Open
lzpong opened this issue Jul 13, 2018 · 0 comments
Open

获取给js文件传的参数 #16

lzpong opened this issue Jul 13, 2018 · 0 comments

Comments

@lzpong
Copy link
Owner

lzpong commented Jul 13, 2018

html中 通过 <script type="text/javascript" src=""></script> 引入的js文件 传递的参数,怎么获取呢?

通过下面这个函数可以获取:

//获取Js文件传参,返回json
var getJsFileArgs=(function(){
    var sc=document.getElementsByTagName('script');
    var paramsArr=sc[sc.length-1].src.split('?')[1].split('&');
    var args={},argsStr=[],param,t,name,value;
    for(var ii=0,len=paramsArr.length;ii<len;ii++){
        param=paramsArr[ii].split('=');
        name=param[0],value=param[1];
        if(typeof args[name]=="undefined"){ //参数尚不存在
            args[name]=value;
        }else if(typeof args[name]=="string"){ //参数已经存在则保存为数组
            args[name]=[args[name]]
            args[name].push(value);
        }else{  //已经是数组的
            args[name].push(value);
        }
    }
    return function(){return args;} //以json格式返回获取的所有参数
})();

测试(html文件和js文件)

//获取Js文件传参,返回json
var getJsFileArgs=(function(){
    var sc=document.getElementsByTagName('script');
    var paramsArr=sc[sc.length-1].src.split('?')[1].split('&');
    var args={},argsStr=[],param,t,name,value;
    for(var ii=0,len=paramsArr.length;ii<len;ii++){
        param=paramsArr[ii].split('=');
        name=param[0],value=param[1];
        if(typeof args[name]=="undefined"){ //参数尚不存在
            args[name]=value;
        }else if(typeof args[name]=="string"){ //参数已经存在则保存为数组
            args[name]=[args[name]]
            args[name].push(value);
        }else{  //已经是数组的
            args[name].push(value);
        }
    }
    return function(){return args;} //以json格式返回获取的所有参数
})();

alert(JSON.stringify(getJsFileArgs()));
alert("username:"+getJsFileArgs()["username"]);
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title> new document </title>
  <meta name="generator" content="editplus" />
  <meta name="author" content="" />
  <meta name="keywords" content="" />
  <meta name="description" content="" />
  <script type="text/javascript" src="test.js?id=4&username=yemoo&id=1&uid=110"></script>
  <script type="text/javascript" src="test.js?id=5&username=ajaxbbs&id=7&uid=253"></script>
  <script type="text/javascript" src="test.js?id=6&username=jack&id=8&uid=258"></script>
</head>
<body>
</body>
</html>

tim 20180713181832

参考:

  1. https://blog.csdn.net/a9529lty/article/details/4168916
  2. http://www.jiaonan.tv/html/blog/1/27731.htm
  3. http://www.cnblogs.com/sking7/archive/2011/11/23/2260572.html (较全)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant