How to fix the resize event in IE

By , 2007-09-23 22:45:46

In IE the window resize event is fired multiple times per actual resize: it is a well known issue for IE6 and IE7, but they misbehave along different patterns. Actually it seems that IE6 is worse than IE7.

After quite a long session of R&D, I’ve got to a pretty good solution, in the form of a jQuery plugin: jquery.wresize.js

/*  
===============================================================================
WResize is the jQuery plugin for fixing the IE window resize bug
...............................................................................
                                               Copyright 2007 / Andrea Ercolino
-------------------------------------------------------------------------------
LICENSE: http://www.opensource.org/licenses/mit-license.php
WEBSITE: http://noteslog.com/
===============================================================================
*/

( function( $ ) 
{
	$.fn.wresize = function( f ) 
	{
		version = '1.1';
		wresize = {fired: false, width: 0};

		function resizeOnce() 
		{
			if ( $.browser.msie )
			{
				if ( ! wresize.fired )
				{
					wresize.fired = true;
				}
				else 
				{
					var version = parseInt( $.browser.version, 10 );
					wresize.fired = false;
					if ( version < 7 )
					{
						return false;
					}
					else if ( version == 7 )
					{
						//a vertical resize is fired once, an horizontal resize twice
						var width = $( window ).width();
						if ( width != wresize.width )
						{
							wresize.width = width;
							return false;
						}
					}
				}
			}

			return true;
		}

		function handleWResize( e ) 
		{
			if ( resizeOnce() )
			{
				return f.apply(this, [e]);
			}
		}

		this.each( function() 
		{
			if ( this == window )
			{
				$( this ).resize( handleWResize );
			}
			else
			{
				$( this ).resize( f );
			}
		} );

		return this;
	};

} ) ( jQuery );

If you want to try it, here is a test page, where a div is automatically resized when the window is resized.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" style="overflow:hidden;">
 <head>
  <title> test window resize </title>

<script type="text/javascript" src="http://jquery.com/src/jquery-latest.pack.js"></script>
<script type="text/javascript" src="jquery.wresize.js"></script>


<script type="text/javascript">
jQuery( function( $ ) 
{
	function content_resize() 
	{
		var w = $( window );
		var H = w.height();
		var W = w.width();
		$( '#content' ).css( {width: W-20, height: H-20} );
	}

	$( window ).wresize( content_resize );

	content_resize();
} );
</script>

 </head>

 <body>
 
<div id="content" style="border: 1px dashed silver; position:absolute; overflow:auto;">
test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test 
</div>

 </body>
</html>

References:

http://dev.jquery.com
http://snook.ca
http://ecmascript.stchur.com

Leave a Reply

42 Responses to “How to fix the resize event in IE”

  1. Art says:

    Hello,
    I did exactly as in your example but in IE8 the function in wresize event is called incrementally on and on.

  2. Renuka says:

    Thanks for your code. It helps me a lot.

  3. Robert D. Cluett says:

    Pretty much had the same solution…

    var dashboard = {

    ieresizeTimer : null,

    setSizeWrapper : function()
    {
    if (client.isIE)
    {
    $(‘resizemask’).style.display = ‘block’;
    clearTimeout(dashboard.ieresizeTimer);
    dashboard.ieresizeTimer = setTimeout(“dashboard.setSize()”,100);
    }
    else
    {
    dashboard.setSize();
    }
    },

    setSize : function()
    {
    // resize code here
    }

  4. Andrea Ercolino says:

    @Steve, many thanks. There was something wrong with my post template, still to understand what exactly, but I could fix it.

  5. Steve says:

    It seems that you have turned overflow on this site to hidden or somehow turned off scrollbars. Looking at this in Chrome 6. Thanks for the post.

  6. Tchat says:

    Thank you for work :-)

  7. Ron C says:

    I’m seeing another IE8 quirk with resize. The HTML page has no width set and uses the jQuery scroll with a timer as above to reposition an absolute DIV if it has gone off-screen.

    Under IE8, when the window is reduced in width, IE briefly inserts a horizontal scroll bar (reducing the height). It appears to then send separate resize events for the reduction in width and height, then another appreciably later for the increase in height when it removes the horizontal scroll bar it just added! This does not happen under Firefox, Safari, nor Chrome.

    I can increase the delay started the resize events to mask this. About 500mS does the trick on my test machine, but have doubt that will work on a broad spectrum of machines. It also makes the app look a bit unresponsive, though it’s better than seeing the div glide in from the right, then drop down from the top under IE.

    Grrr.

  8. estetik says:

    very good article thanks admin for the post

  9. Hi everyone, a little bit late, but I believe both Hari and Aamir are valid solutions.

    In my case I needed to make sure no repeated events were fired and also check if the only the width has changed. Here is the code I used, based on both solutions:

    var windowResizeTimeout;
    var tempCounter = 0;
    var winWidth;

    function DelayedResize() {
    window.status = ++tempCounter;
    var winNewWidth = $(window).width();
    if (winWidth != winNewWidth)
    UpdateAllSizes(); // function to process the resize to the controls on the page.
    winWidth = winNewWidth;
    }

    function checkResize() {
    window.clearTimeout(windowResizeTimeout);
    windowResizeTimeout = window.setTimeout(DelayedResize, 250);
    }
    $(window).wresize(checkResize);

    Regards

  10. pengkai says:

    @Hari YEP,your function is useful. like to handle dbclick in javascript. Don’t to use plug-ins is better. :)

  11. Aamir Afridi says:

    Hello guys I just discovered another problem which might help you.

    I am using jQuery and I have window.resize event to call a function which will re-position the div appended to the body.

    Now when I set the LEFT css property of that appended div, the window.resize event get trigger for NO GOOD REASON.

    It it results in an infinite loop, triggering the window.resize again and again.

    The code without fix:
    =============

    $(window).resize(function(){

    onResize = function() {
    //The method which sets the LEFT css property which triggers window.resize again and it was a infinite loop
    setWrapperPosition($mainWrapper.parent());
    }
    window.clearTimeout(resizeTimeout);
    resizeTimeout = window.setTimeout(onResize, 10);
    });

    Solution:
    =====

    var winWidth = $(window).width(),
    winHeight = $(window).height();

    $(window).resize(function(){

    onResize = function() {
    //The method which sets the LEFT css property which triggers window.resize again and it was a infinite loop
    setWrapperPosition($mainWrapper.parent());
    }

    //New height and width
    var winNewWidth = $(window).width(),
    winNewHeight = $(window).height();

    // compare the new height and width with old one
    if(winWidth!=winNewWidth || winHeight!=winNewHeight)
    {
    window.clearTimeout(resizeTimeout);
    resizeTimeout = window.setTimeout(onResize, 10);
    }
    //Update the width and height
    winWidth = winNewWidth;
    winHeight = winNewHeight;
    });

    So basically it will check if the height or width is changed (which will happen ONLY when you actually resize with window).

  12. djuRa says:

    Thanks Hari…Very, Very nice

  13. ollie says:

    What a life saver!

  14. Hari says:

    My apologies: add the following at the LAST statement of DelayedResize.

    window.clearTimeout(windowResizeTimeout);

  15. Hari says:

    I’m sorry if I sound negative, but I think that the following is a way better solution to for those who are looking into NOT executing the same code too many times. The “DelayedResize” method bellow will execute multiple times before user releases the mouse (e.g. drag window size handle, then pause without releasing the mouse), but not even nearly as many times as the onResize fires by default. And, this does not change Mozilla’s behavior at all.

    $(document).ready(function()
    {
    var tempCounter = 0;
    var windowResizeTimeout;

    window.onresize = function()
    {
    window.clearTimeout(windowResizeTimeout);
    windowResizeTimeout = window.setTimeout(DelayedResize, 100);
    };

    function DelayedResize()
    {
    window.status = ++tempCounter;
    // Add your original window.onresize implementation here
    }
    }

  16. Michael says:

    Great plugin. Saved me from a serious headache with IE/FF resizing! I did have some trouble copying and pasting your code. Whatever you’re using to display code on this page is a bit buggy. Thanks again!

  17. Cujo says:

    I’m also not sure I understand how this is supposed to work. I’m developing a plugin for generic resizing (of windows, images, etc.). When I enter the plugin using the regular jQuery event handler method — $(selector).resize(function(){…$(selector).plugin(options)…} — jQuery gives a “this” object with at least one member. If instead I enter the plugin using wresize — $(selector).wresize($(selector).plugin(options)) — the plugin sees a “this” object with this.length == 0. Shouldn’t the plugin get identical “this” objects from jquery’s resize and wresize?

  18. Hugh says:

    ABSOLUTELY BRILLIANT!
    I have been trying to work around the IE Resize Bug many times, many hours and for many different projects!
    Finally, thanks to you, I have a solution which I can tailor for every need!!!
    I really owe my thanks to you and hope that others find it as useful as I did!

  19. Steven says:

    thanks

  20. Raymond Shaw says:

    Thank you :)

Panorama Theme by Themocracy