Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does IE8 hangs on jquery window.resize event?

I discovered a problem that seems to reproduce always when opening a piece of html and javascript in IE8.

<html>
    <body>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $(window).resize(function() {
                console.log('Handler for .resize() called');
            });
        });
    </script>
    <div id="log">
    </div>
    </body>
</html>

Loading this file in IE8 and opening Developer Tools will show that the log message is printed continuously after one resize of the browser window.

Does anyone has an idea why? This is not happening in IE7 or IE9, nor in other browsers (or at least their latest versions).

UPDATE

One solution to prevent the continuos trigger of resize() is to add handler on document.body.onresize if the browser is IE8.

 var ieVersion = getInternetExplorerVersion();
    if (ieVersion == 8) {
        document.body.onresize = function () {
        };
    }
    else {
        $(window).resize(function () {
        });
    }

But this does not answer my question: is the continuous firing of resize() a bug in IE8?

like image 261
Vasile Tomoiaga Avatar asked May 10 '11 08:05

Vasile Tomoiaga


1 Answers

If "show window contents while dragging" is switched on, you will be inundated with resize events. I guess you're testing IE8 on a separate Windows machine which has this effect enabled (Display Properties -> Appearance -> Effects...).

To counteract this, you can wrap & trap the resize events to tame them: http://paulirish.com/demo/resize

This article says Chrome, Safari & Opera suffer from this too.

like image 186
Lee Kowalkowski Avatar answered Sep 29 '22 10:09

Lee Kowalkowski