当前位置:Gxlcms > JavaScript > IE下window.onresize多次调用与死循环bug处理方法介绍_javascript技巧

IE下window.onresize多次调用与死循环bug处理方法介绍_javascript技巧

时间:2021-07-01 10:21:17 帮助过:29人阅读

window.onresize 在IE浏览器下有多次被执行甚至死循环的bug,会导致浏览器卡死

解决方案:

代码如下:

// IE浏览器下将onresize事件放在div上
if(navigator.userAgent && navigator.userAgent.toLowerCase().indexOf("msie")>-1){
document.body.innerHTML = '
' + document.body.innerHTML;
} else {
window.onresize = function(){
resetClientWidth();
lazyLoadImgesMethod();
};
}

ps:FF浏览器不支持在div上写onresize事件,window.onresize 虽然在FF下不会死循环,但如果在函数里面有alert执行,FF浏览器也会卡死,很无奈的bug。

PS: navigator.userAgent.split(";")[1].toLowerCase().indexOf("msie")>-1 这个判断ie的问题, 在chrome浏览器下会出现"toLowerCase()"方法未定义的错误.

所以修正为navigator.userAgent.toLowerCase().indexOf("msie")>-1

人气教程排行