0

I want to send some data via postMessage from a page to another one which have different domains. However, I cannot achieve that since the code inside $(yo.document).load never runs; I tried the commented version as well. Here is my code:

<a onclick="popupCenter('http://localhost:58810');" href="javascript:void(0);">CLICK</a>
<script>

    function popupCenter(url) {
        const yo = window.open(url);
        $(yo.document).load(function() {
        //yo.document.onload = function() {
            console.log("yo loaded");
            yo.postMessage("Hello mate", "*");
        });
    } 
</script>
</body>
</html>

The new window opens normally, however the callback inside load is not called. Any ideas?

Unknown developer
  • 5,414
  • 13
  • 52
  • 100
  • Possible duplicate of [Detecting when Iframe content has loaded (Cross browser)](https://stackoverflow.com/questions/751435/detecting-when-iframe-content-has-loaded-cross-browser) – CBroe May 04 '18 at 11:34
  • I don’t think you can attach a load event handler to an iframe cross-domain ... However, if only the port differs, you could perhaps get away with setting `document.domain`, https://stackoverflow.com/a/2754508/1427878 – CBroe May 04 '18 at 11:35
  • 1
    I'm not sure that you can detect a page load from another tab or window. Off the top of my head, you can use `postMessage` in your popup script after the DOM has loaded there back to your parent page to indicate that it's DOM has been loaded. You can then postMessage as you have done in your handler from your parent page to the popup – Chirag Ravindra May 04 '18 at 11:36
  • Can you try `yo.document.readyState === "complete"`? – Matt May 04 '18 at 11:37
  • I tried `setTimeout(yo.postMessage.bind(this,"Hello mate", "*"), 5000);` as well. Unfortunately, it does not work and I do not know why. – Unknown developer May 04 '18 at 11:48
  • Am i see this wrong, or you're trying to attach an event to another webpage,if the other page is yours, i would suggest you add some back-end api calls, and have the load event on the other page listening for those calls. – Rainbow May 04 '18 at 12:32

0 Answers0