Home >Web Front-end >JS Tutorial >How Can I Monitor the onLoad Event in Pop-ups Created Using window.open?

How Can I Monitor the onLoad Event in Pop-ups Created Using window.open?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-24 13:35:31256browse

How Can I Monitor the onLoad Event in Pop-ups Created Using window.open?

Monitoring onLoad Event in Pop-up Windows Created with window.open

Detecting the onLoad event in a window opened using window.open presents a challenge in various browsers. The following code attempts to implement this, but fails:

window.popup = window.open($(this).attr('href'), 'Ad', 'left=20,top=20,width=500,height=500,toolbar=1,resizable=0');
$(window.popup).onload = function()
{
    alert("Popup has loaded a page");
};

Solutions

To successfully capture the onLoad event, the following methods are recommended:

1. addEventListener

For modern browsers, utilize the addEventListener method as follows:

var myPopup = window.open(...);
myPopup.addEventListener('load', myFunction, false);

2. attachEvent (for IE)

If supporting Internet Explorer is crucial, employ the attachEvent method:

myPopup[myPopup.addEventListener ? 'addEventListener' : 'attachEvent'](
  (myPopup.attachEvent ? 'on' : '') + 'load', myFunction, false
);

Caution for IE Support

Supporting IE can be cumbersome. If possible, consider avoiding it or implementing specific solutions for IE compatibility.

The above is the detailed content of How Can I Monitor the onLoad Event in Pop-ups Created Using 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