We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
<script type="text/javascript" src=""></script>
通过下面这个函数可以获取:
//获取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格式返回获取的所有参数 })();
//获取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>
参考:
The text was updated successfully, but these errors were encountered:
No branches or pull requests
html中 通过
<script type="text/javascript" src=""></script>
引入的js文件 传递的参数,怎么获取呢?通过下面这个函数可以获取:
测试(html文件和js文件)
参考:
The text was updated successfully, but these errors were encountered: