我对Javascript的网址参数有疑问......
假设我有:
<script src="http://example.com/javascript.js?param=true"></script>
我如何接受或接收&#34; param&#34; javascript.js脚本中的参数结束。
答案 0 :(得分:2)
试试这个:
<script>
var param1var = getQueryVariable("param1");
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
alert('Query Variable ' + variable + ' not found');
}
</script>
答案 1 :(得分:1)
<script src="my.js?myvar=123"></script>
并在你的js文件里面,
var myTags=document.getElementsByTagName("script");
var src= myTags[myTags.length-1].src;
var state=unescape(src).split("myvar=")[1].split("&")[0];
alert(state);