1

I'm going to use postMessage method to send a text to an opened window right after opening the window on ther origin. I tried the following:

let ref = window.open("<address>", "name", "resizable,scrollbars,status");
ref.postMessage("Some Message");

A listener is defined on the other page to get the posted message, but it doesn't work, since the posted message is being sent before the page loading is completed.

Is there any way to check that the page on the opened window is fully loaded?

Super Hornet
  • 2,839
  • 5
  • 27
  • 55

3 Answers3

3

You can implement an "handshake" mechanism, so it would be like,

  1. parent adds an event listener to catch child messages (window.addEventListener('message', () => {}))
  2. parent opens child window
  3. child window sends "I'm a live" message to parent (parent.window.opener.postMessage('ImALive', '<url address of parent>'))
  4. parent sends it's data to child

Something like that.

Super Hornet
  • 2,839
  • 5
  • 27
  • 55
felixmosh
  • 32,615
  • 9
  • 69
  • 88
0

You can try simple Javascript trick as below to see if DOM is loaded, you can use this function which will get execute after DOM gets load.

document.addEventListener('DOMContentLoaded', function(event) {
   //alert("hello"); your code
})
SylvesterTheKid
  • 75
  • 1
  • 14
  • If you are using jQuery library in your code then your life will be more simpler there are many examples using jQuery for waiting to execute the code after loading DOM – SylvesterTheKid May 21 '19 at 07:20
  • I tried that, It doesn't work like that. Because, the message is received before loading the page. – Super Hornet May 21 '19 at 07:23
  • Okay, is there any restriction like you have to use listeners only or you can use HTML local-storage to set the value and get it on other page... will be easier. – SylvesterTheKid May 21 '19 at 07:27
  • 1
    since the parent and child pages are living on different doamins, they cannot have access to each other resources. – Super Hornet May 21 '19 at 07:29
0

Have you tried $(window).load(function(){}).

You can write your code in this event and then try it.

This question on SO might help.

Code_Ninja
  • 1,729
  • 1
  • 14
  • 38