2017-04-23 120 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」這是我用的,還是一個例子? –