Home  >  Article  >  Web Front-end  >  How to Detect Page Load Events in Windows Opened with Window.open?

How to Detect Page Load Events in Windows Opened with Window.open?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-24 13:05:31683browse

How to Detect Page Load Events in Windows Opened with Window.open?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn