getURLParameter用于登陆页面

时间:2017-04-23 11:40:34

标签: javascript jquery html

我试图在我的目标网页上显示网址参数,但它无效。



<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>
&#13;
&#13;
&#13;

我登陆的所有内容都是:

您可能感染了病毒

当我强制url中的值:

这是网址

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

什么都没发生

非常感谢任何帮助

1 个答案:

答案 0 :(得分: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>

相关问题