if(location.href.indexOf("?")>-1) { var params = location.href.substr(location.href.indexOf("?")+1).split("&"); for (var i=0;i<params.length;i++){ var kv = params[i].split("="); var key = kv[0]; var value = kv[1]; console.log("key="+key+" value="+value); } }
同理,可以先获取script标签中的src属性,再通过上面的方法,获取layout参数
1
<scriptsrc="listen.js?layout=right"></script>
在js插件文件中通过如下代码获取
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
var layout="right"; var scripts = document.getElementsByTagName("script"); for (var i=0; i< scripts.length;i++) { var script = scripts[i]; if (script && script.getAttribute("src") && script.getAttribute("src").indexOf("listen.js")>-1) { var url = script.getAttribute("src"); var params = url.substr(url.indexOf("?")+1).split("&"); for (var i=0;i<params.length;i++){ var kv = params[i].split("="); var key = kv[0]; var value = kv[1]; if("layout" == key) { layout = value; } } } } console.log(layout);