Search code examples
javascriptiframepostmessage

Change src of parent window iframe


Suppose My Website is https://example.com I have loaded iframe inside that having src https://example2.com/iframe

When my Iframe is loaded, then inside that iframe i have a button which redirect to https://example3.com. so example3 doesn't allow to open inside Iframe, so my code is failing.

For workaround of this, I decided that I will open https://example3.com in another window and will do some stuff, close that window. Next, I will return to https://example.com (Website in which iframe has loaded) and wanted to add one params to my iframe's src, so that i can identify that i have completed my task of https://example3.com and want to change and reload the src of Iframe.

Please help me how can i solve this. Please suggest some other better way as well if possible.

Please feel free to add the comment in case i can help you to understand my problem better.


Solution

  • Since it sounds like you don't control the code on the site in new window about the only practical thing you can do would be to set an interval to check if the new window you opened with window.open() is closed from the page you opened it.

    Then once you determine it is closed add a prompt of some sort for the user

    var otherWindow = window.open('example3.com');
    
    var timer = setInterval(function(){
       if(otherWindow.closed){
          clearInterval(timer);
          if(confirm('Did you complete that step?')){
            // adjust iframe url
          }
       }
    },1000);// check every second if it is closed