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

How to Detect Window Load Events in Child Windows Opened via Window.open?

Linda Hamilton
Release: 2024-10-24 14:33:02
Original
398 people have browsed it

How to Detect Window Load Events in Child Windows Opened via Window.open?

Detecting Window Load Events for Windows Opened via window.open

Detecting the load event of a window opened using window.open is essential for communication between the parent and child windows. While the approach outlined in the initial code snippet doesn't work in many browsers, here is a robust solution:

Using the addEventListener Method:

<code class="javascript">var myPopup = window.open(...);
myPopup.addEventListener('load', myFunction, false);</code>
Copy after login

This method is supported by all major browsers, including IE, Firefox, and Chrome.

Supporting IE with attachEvent:

If supporting IE is crucial, the following code can be used instead of addEventListener:

<code class="javascript">myPopup[myPopup.addEventListener ? 'addEventListener' : 'attachEvent'](
  (myPopup.attachEvent ? 'on' : '') + 'load', myFunction, false
);</code>
Copy after login

This approach provides backward compatibility with older versions of IE.

Note on IE Support:

Extending support for IE can be cumbersome. Consider using alternative approaches if possible. However, if your audience requires IE support, the attachEvent method is a reliable solution.

The above is the detailed content of How to Detect Window Load Events in Child Windows Opened via 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!