javascript - getURLParameter 用于登陆页面

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

我在登陆页面上得到的只是:

您可能感染了病毒

当我强制使用 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/

相关文章:

javascript - 在javascript中设置多值参数

jquery - 在 Jquery 中为 .animate 设置边界

html - 无法插入按钮 "on top"

html - Tailwindcss 遇到问题

html - 如何通过 CSS 选择和更改下拉列表 <select> 中的值

javascript - 从 rest node.js 压缩 json 数据使用快速压缩

javascript - 未捕获的类型错误 : Object (JS Function) has no method 'apply'

javascript - 禁止文件夹 1 从文件夹 2 导入任何内容

JQuery 和 Ajax : Cannot read property 'length' of undefined

javascript - 更改 Jquery 工具选项卡幻灯片可点击区域