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

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

Linda Hamilton
Linda HamiltonOriginal
2024-10-24 14:33:02399browse

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>

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>

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!

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