简体   繁体   中英

why window.onload not fire in IE

I have some code to open a popup, then resize the it via certain element on the popup page. The following code work fine in Chrome and Firefox, but not IE.

 var newWin; function openWindow(id) { newWin = window.open(url,config,setting); newWin.onload= function () { newWin.resizeTo(newWin.document.getElementById("certainID").offsetWidth,100); };

In IE10 a popup will come but the window.load event will never fire. I also try newWin.$(document).ready , but seems it is invalid.

Any suggestion?

This is an old problem in IE. One of the best solution is to add new .

Normally we write

window.onload=function() { alert('hello');};

Replace it with

window.onload=new function() { alert('hello');};

Finally I resolve this via setTimeout to check whenever the element in popup is well-generated, then resize the popup window

 var newWin; function openWindow(id) { newWin = window.open(url,config,setting); var resizePopup = function () { newWin.resizeTo(newWin.document.getElementById("certainID").offsetWidth,100); newWin.focus(); }; var tryResize = function(){ if(newWin.document.getElementById("certainID")==null){ setTimeout(function(){tryResize();},500); } else{ resizePopup(); } } tryResize(); };

If using setTimeout, may be popup perform resizing when window not finish loading yet. I think best solution is using setInterval and check whenever document.readyState==='complete" => call clearInterval() and run resize

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM