Home > Web Front-end > JS Tutorial > body text

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

Mary-Kate Olsen
Release: 2024-10-24 13:05:31
Original
682 people have browsed it

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);
Copy after login

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
);
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!