Detecting Page Load Events in Windows Opened with window.open
In some instances, it becomes necessary to detect when a page has fully loaded in a window created using the window.open method. However, attempting to capture this event using $(window.popup).onload does not prove successful. This article delves into alternative techniques that allow developers to effectively monitor page load events in windows opened through window.open.
The key to achieving this task lies in utilizing the addEventListener method. Here's an updated code snippet that addresses the issue:
var myPopup = window.open(...); myPopup.addEventListener('load', myFunction, false);
This code assigns a dedicated event listener to the window object, which then triggers the myFunction when the page within the opened window has finished loading.
Ensuring Compatibility with IE
It's important to note that supporting Internet Explorer (IE) requires a slightly different approach:
myPopup[myPopup.addEventListener ? 'addEventListener' : 'attachEvent']( (myPopup.attachEvent ? 'on' : '') + 'load', myFunction, false );
This extended syntax adds compatibility with IE by leveraging both the addEventListener and attachEvent methods.
While supporting IE can be cumbersome, it is recommended to do so only when absolutely necessary due to audience considerations.
The above is the detailed content of How to Detect Page Load Events in Windows Opened with Window.open?. For more information, please follow other related articles on the PHP Chinese website!