Detecting the Onload Event of a Window Opened with Window.Open

Detecting the onload event of a window opened with window.open

If the pop-up's document is from a different domain, this is simply not possible.

Update April 2015: I was wrong about this: if you own both domains, you can use window.postMessage and the message event in pretty much all browsers that are relevant today.

If not, there's still no way you'll be able to make this work cross-browser without some help from the document being loaded into the pop-up. You need to be able to detect a change in the pop-up that occurs once it has loaded, which could be a variable that JavaScript in the pop-up page sets when it handles its own load event, or if you have some control of it you could add a call to a function in the opener.

Onload event is not firing after passing Html string through window.open in Angular 8

When using document.write(), you have to call document.close() afterwards to finalize the document, otherwise the load event will not be fired.

const win = window.open('', '_blank', 'left=50,top=50,width=1334,height=700');
win.document.write(htmlData);
win.document.close();

windows popup onload event not working Javascript

Since it's not the same domain , you will not be able to access the window set properties.

Try this here in console

(function(ow) {
ow.addEventListener("load", function() { alert("loaded"); }, false);

})(window.open('https://stackoverflow.com/', 'example', 'width=300,height=300'));

This will work

But if you put a different domain , it will not work. You'd have to use PostMessage or something else.

how to detect window.open

You can replace it

NOTE: window.open does not work at stackoverflow due to sandboxing
Also a window.open not triggered by a user action will likely be suppressed by the browser

const saveOpen = window.open;
window.open = function(url,windowName,parms) {
console.log("triggered",url);
saveOpen(url, windowName || "",parms || "")
};
const url = "https://stackoverflow.com", windowName="myWin"
setTimeout(function(){
window.open(url,windowName);
}, 1000);

window.onload not firing when window is already open

As I stated in my comment, if the window is already open, what do you expect it's load function to do? It's already loaded. Close the window and it loads the window again.

You can use the window.close function to close the window and then open it again.

wconsole.close()


Related Topics



Leave a reply



Submit