2017-04-23 118 views
0

我试图让我的着陆页上显示的URL参数,但它不工作。getURLParameter登陆页面

<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>

我得到的着陆页是:

你可能与病毒感染

当我强制值网址:

这是网址

www.domain.com/page.html?devicebrand=Apple&devicemodel=Iphone 

什么也没有发生

任何帮助是极大的赞赏

回答

1

工作对我来说,如果我要确保我通过一些并加载页面时运行。不要使用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>

+0

感谢您的回答mplungjan,我绝对新手,我应该把我的文字“你可能与病毒感染”? –

+0

该文本现在可以在任何地方。我的代码通常存储在脚本标记 – mplungjan

+0

好吧,我明白!谢谢 我测试了我的android上的新链接现在它仍然说iphone虽然 var loc =“#devicebrand = iPhone&devicemodel = 6S”这是我用的,还是一个例子? –