【问题标题】:Can't pass my parameters in iframe无法在 iframe 中传递我的参数
【发布时间】:2018-07-11 07:02:17
【问题描述】:

我在 iframe 中有一个带有表单的父页面: https://profiel.pelckmansuitgevers.be/?email=dennis@hybridmedia.be

表单的所有字段都应预先填写。但这不再起作用了。

如果给url加上email参数,这个参数会被添加到iframe的source中。

但在我的 iframe 上,我无法获取电子邮件参数。 我在 iframe 中这样做:

var urlParams = new URLSearchParams(window.location.search);
if (urlParams.has('email')) {
    var email = urlParams.get('email');

    //all my code here...
}

urlParams 好像是空的。

但是当我以隐身模式(chrome)打开页面并进行硬刷新时,所有字段都已预填充。所以它适用于这种情况。

有谁知道问题出在哪里? 也许我的脚本正在尝试接收电子邮件参数,但那一刻不存在?还是别的什么?

谢谢!

【问题讨论】:

  • 就我而言,我只是单击了您的链接profiel.pelckmansuitgevers.be/?email=dennis@hybridmedia.be,并且 iframe 中的所有字段都已正确填写。我在 Chrome 中,而不是在隐身模式下
  • 我已经解决了你的问题,请看下面我的回答
  • 对于其他 stackoverflow 用户:问题已解决;-)

标签: javascript jquery iframe parameters


【解决方案1】:

你似乎有两个问题:

1) 您的 iframe 位于子域中,并且您未在 iframe url 中发送 email 参数。因此,某些浏览器(例如 Google Chrome)将无法访问该电子邮件。

解决此问题的示例代码:

  var loc = window.location.toString(),
  params = loc.split('?')[1],
  iframe = document.getElementById('updateform');

  iframe.src = "https://pelckmans.houston-1.hybridmedia.be/update/pelckmansuitgevers/" + '?' + params + '&v=' + Date.now();

2) JS缓存问题


你可以通过改变来解决这个问题

<script src="js/prefill.js"></script>

    <script>document.write("<script type='text/javascript' src='js/prefill.js.php?v=" + Date.now() + "'><\/script>");</script>

在您的 iframe 页面中https://pelckmans.houston-1.hybridmedia.be/update/pelckmansuitgevers/


当您开发 JS 并将其包含在 .js 文件中时,经常会发生此问题。

浏览器缓存 js...解决这个问题的一种方法是在 js 文件名之后添加时间戳,或者像我在代码中那样添加版本号。

您可以在此线程 How to append timestamp to the java script file in <script> tag url to avoid caching 中找到更多方法来执行此操作

【讨论】:

  • 我已将参数添加到脚本中,但这不起作用:(
  • 您还必须更改 iframe 网址! ;-) 在 iframe url 中添加“?version=2”。我已经更新了上面的答案
  • 不抱歉,两个网址都已更改。
  • 出现了一次。刷新后,不会再次预填充任何字段。
  • 在浏览器中加载profiel.pelckmansuitgevers.be/… ("email=dennis@hybridmedia.be&version=2") ...现在应该可以了
猜你喜欢
  • 2021-12-20
  • 1970-01-01
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 2015-01-28
  • 2014-11-29
  • 1970-01-01
相关资源
最近更新 更多