简体   繁体   中英

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

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.

But on my iframe, I cannot get the email parameter. I'm doing this in the 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.

But when I open the page in incognito mode (chrome) and do a hard refresh, all the fields are prefilled. 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? Or something else?

Thanks!

You seem to have 2 problems :

1) your iframe is in a subdomain , and you don't send the email parameter in the iframe url. So some browsers like Google Chrome won't be able to access the email.

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


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/


This problem happens often when you develop JS and includes this in a .js file.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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