简体   繁体   中英

Javascript SetInterval Not Working in IE

I am using window.open to open a popup window and setinterval function to wait and refresh the background page, once the popup is closed. the code is working fine in Chrome and Firefox but is not working in IE.

Basically the issue is: in IE, it doesn't wait until popup window is closed. It immediately gets refreshed as soon as the popup opens up. I saw the issue coming in both IE 9 & IE 11.

Any solution for this?

This is the code:

var url = "/apex/VFP_Add";
var win = window.open(url, "Add" ,"width=600, height=300, scrollbars=yes"); 
win.moveTo(500, 100); 
win.focus(); 
var timer = setInterval(function() { 
if(win.closed) { 
clearInterval(timer); 
window.location.reload(); 
} 
}, 500);

I put alerts just before if(win.closed) check and just after the check. For the first alert, it showed as False. In second alert, after "if check", it showed True. This is very weird because i had not closed the window.

It seems that's a known bug in IE. See this article about it: https://support.microsoft.com/en-us/kb/241109

Their solution is to basically negate the value of win.closed when you detect that it's running in IE. Something like:

if(win.closed || isRunningInIE()) {
    clearInterval(timer); 
    window.location.reload(); 
}

There are different ways to detect IE, so you can just use your favorite method in place of that isRunningInIE() function.

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