Super Hornet
Super Hornet

Reputation: 2907

How a page can be determined if it's loaded

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?

Upvotes: 1

Views: 93

Answers (3)

felixmosh
felixmosh

Reputation: 35513

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.

Upvotes: 3

Code_Ninja
Code_Ninja

Reputation: 1877

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

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

This question on SO might help.

Upvotes: 0

SylvesterTheKid
SylvesterTheKid

Reputation: 75

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
})

Upvotes: 0

Related Questions