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"); };
To successfully capture the onLoad event, the following methods are recommended:
For modern browsers, utilize the addEventListener method as follows:
var myPopup = window.open(...); myPopup.addEventListener('load', myFunction, false);
If supporting Internet Explorer is crucial, employ the attachEvent method:
myPopup[myPopup.addEventListener ? 'addEventListener' : 'attachEvent']( (myPopup.attachEvent ? 'on' : '') + 'load', myFunction, false );
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!