简体   繁体   English

无法在iframe中传递参数

[英]Can't pass my parameters in iframe

I have a parent page with a form in an iframe: https://profiel.pelckmansuitgevers.be/?email=dennis@hybridmedia.be 我在iframe中有一个带有表单的父页面: https : //profiel.pelckmansuitgevers.be/?email=dennis@hybridmedia.be

All the fields of the form should be prefilled. 表格的所有字段均应预先填写。 But that doesn't work anymore. 但这不再起作用。

If you add the email parameter to the url, this parameter is added to the source of the iframe. 如果您将email参数添加到url,则此参数将添加到iframe的源。

But on my iframe, I cannot get the email parameter. 但是在我的iframe上,我无法获取email参数。 I'm doing this in the iframe: 我在iframe中这样做:

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

    //all my code here...
}

It seems that urlParams is empty. 似乎urlParams为空。

But when I open the page in incognito mode (chrome) and do a hard refresh, all the fields are prefilled. 但是,当我以隐身模式(chrome)打开页面并进行硬刷新时,所有字段都被预先填充。 So it works in this case. 因此,它在这种情况下有效。

Does anyone know what the problem is? 有人知道问题出在哪里吗? Maybe that my script is trying to receive the email parameter but that this doesn't exist at that moment? 也许我的脚本正在尝试接收email参数,但是那一刻不存在? Or something else? 或者是其他东西?

Thanks! 谢谢!

You seem to have 2 problems : 您似乎有2个问题:

1) your iframe is in a subdomain , and you don't send the email parameter in the iframe url. 1) 您的iframe位于子域中 ,并且您没有在iframe网址中发送email参数。 So some browsers like Google Chrome won't be able to access the email. 因此,某些浏览器(例如Google Chrome浏览器)将无法访问该电子邮件。

Sample code for solving this problem : 解决此问题的示例代码:

  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 cache problems 2) JS缓存问题


You can solve this with changing 您可以通过更改解决此问题

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

with

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

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


This problem happens often when you develop JS and includes this in a .js file. 当您开发JS并将其包含在.js文件中时,经常会发生此问题。

The browser caches the js...a way to solve this is for instance to add a timestamp after the js filename, or a version number like i did in my code. 浏览器缓存js ...解决此问题的一种方法是,例如在js文件名之后添加时间戳,或者像我在代码中所做的那样添加版本号。

You can find more ways to do this in this thread How to append timestamp to the java script file in <script> tag url to avoid caching 您可以在此线程中找到更多方法来执行此操作。 如何在<script>标记url中将时间戳附加到java脚本文件,以避免缓存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM