我正在尝试获取目标网页上显示的网址参数,但它不起作用。
<script>
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1] || ''
);
}
</script>
<br>
<h1><b>Your <script>document.write(getURLParameter("devicebrand") + (" ") + getURLParameter("devicemodel"))</script>might be infected with <span id="blink"> (6)</span>Viruses!</b></h1>
我在登陆页面上得到的只是:
您可能感染了病毒
当我强制使用 url 中的值时:
这是网址
www.domain.com/page.html?devicebrand=Apple&devicemodel=Iphone
什么也没发生
非常感谢任何帮助
最佳答案
如果我确保传递某些内容并在页面加载时运行,则对我有用。不要使用 document.write
也不要吓到人。
function getURLParameter(name, srch) {
srch = srch || location.search;
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(srch) || [, null])[1] || ''
);
}
window.onload = function() {
var loc = "#devicebrand=iPhone&devicemodel=6S";
document.getElementById("devicebrand").innerHTML = getURLParameter("devicebrand", loc)
document.getElementById("devicemodel").innerHTML = getURLParameter("devicemodel", loc)
}
<br>
<h1><b>Your <span id="devicebrand"></span> <span id="devicemodel"></span> might be infected with <span id="blink"> (6)</span>Viruses!</b></h1>
关于javascript - getURLParameter 用于登陆页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43570685/