Cross-browser window resize event - JavaScript / jQuery

JavascriptJqueryCross BrowserResize

Javascript Problem Overview


What is the correct (modern) method for tapping into the window resize event that works in Firefox, WebKit, and Internet Explorer?

And can you turn both scrollbars on/off?

Javascript Solutions


Solution 1 - Javascript

jQuery has a built-in method for this:

$(window).resize(function () { /* do something */ });

For the sake of UI responsiveness, you might consider using a setTimeout to call your code only after some number of milliseconds, as shown in the following example, inspired by this:

function doSomething() {
    alert("I'm done resizing for the moment");
};

var resizeTimer;
$(window).resize(function() {
    clearTimeout(resizeTimer);
    resizeTimer = setTimeout(doSomething, 100);
});

Solution 2 - Javascript

$(window).bind('resize', function () { 

    alert('resize');

});

Solution 3 - Javascript

Here is the non-jQuery way of tapping into the resize event:

window.addEventListener('resize', function(event){
  // do stuff here
});

It works on all modern browsers. It does not throttle anything for you. Here is an example of it in action.

Solution 4 - Javascript

Sorry to bring up an old thread, but if someone doesn't want to use jQuery you can use this:

function foo(){....};
window.onresize=foo;

Solution 5 - Javascript

Since you are open to jQuery, this plugin seems to do the trick.

Solution 6 - Javascript

Using jQuery 1.9.1 I just found out that, although technically identical)*, this did not work in IE10 (but in Firefox):

// did not work in IE10
$(function() {
    $(window).resize(CmsContent.adjustSize);
});

while this worked in both browsers:

// did work in IE10
$(function() {
    $(window).bind('resize', function() {
        CmsContent.adjustSize();
    };
});

Edit:
)* Actually not technically identical, as noted and explained in the comments by WraithKenny and Henry Blyth.

Solution 7 - Javascript

jQuery provides $(window).resize() function by default:

<script type="text/javascript">
// function for resize of div/span elements
var $window = $( window ),
    $rightPanelData = $( '.rightPanelData' )
    $leftPanelData = $( '.leftPanelData' );

//jQuery window resize call/event
$window.resize(function resizeScreen() {
    // console.log('window is resizing');
    
    // here I am resizing my div class height
	$rightPanelData.css( 'height', $window.height() - 166 );
	$leftPanelData.css ( 'height', $window.height() - 236 );
});
</script> 

Solution 8 - Javascript

I consider the jQuery plugin "jQuery resize event" to be the best solution for this as it takes care of throttling the event so that it works the same across all browsers. It's similar to Andrews answer but better since you can hook the resize event to specific elements/selectors as well as the entire window. It opens up new possibilities to write clean code.

The plugin is available here

There are performance issues if you add a lot of listeners, but for most usage cases it's perfect.

Solution 9 - Javascript

I think you should add further control to this:

	var disableRes = false;
	var refreshWindow = function() {
		disableRes = false;
		location.reload();
	}
	var resizeTimer;
	if (disableRes == false) {
		jQuery(window).resize(function() {
			disableRes = true;
		    clearTimeout(resizeTimer);
		    resizeTimer = setTimeout(refreshWindow, 1000);
		});
	}

Solution 10 - Javascript

hope it will help in jQuery

define a function first, if there is an existing function skip to next step.

 function someFun() {
 //use your code
 }

browser resize use like these.

 $(window).on('resize', function () {
    someFun();  //call your function.
 });

Solution 11 - Javascript

Besides the window resize functions mentioned it is important to understand that the resize events fire a lot if used without a deboucing the events.

Paul Irish has an excellent function that debounces the resize calls a great deal. Very recommended to use. Works cross-browser. Tested it in IE8 the other day and all was fine.

http://www.paulirish.com/2009/throttled-smartresize-jquery-event-handler/

Make sure to check out the demo to see the difference.

Here is the function for completeness.

(function($,sr){

  // debouncing function from John Hann
  // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
  var debounce = function (func, threshold, execAsap) {
      var timeout;

      return function debounced () {
          var obj = this, args = arguments;
          function delayed () {
              if (!execAsap)
                  func.apply(obj, args);
              timeout = null;
          };

          if (timeout)
              clearTimeout(timeout);
          else if (execAsap)
              func.apply(obj, args);

          timeout = setTimeout(delayed, threshold || 100);
      };
  }
  // smartresize 
  jQuery.fn[sr] = function(fn){  return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };

})(jQuery,'smartresize');


// usage:
$(window).smartresize(function(){
  // code that takes it easy...
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBuddyJoeView Question on Stackoverflow
Solution 1 - JavascriptAndrew HedgesView Answer on Stackoverflow
Solution 2 - JavascriptftsView Answer on Stackoverflow
Solution 3 - JavascriptJondlmView Answer on Stackoverflow
Solution 4 - Javascriptjavascript is futureView Answer on Stackoverflow
Solution 5 - JavascriptPaolo BergantinoView Answer on Stackoverflow
Solution 6 - JavascriptbassimView Answer on Stackoverflow
Solution 7 - JavascriptAditya P BhattView Answer on Stackoverflow
Solution 8 - JavascriptoldwizardView Answer on Stackoverflow
Solution 9 - JavascriptFrancesco FrapportiView Answer on Stackoverflow
Solution 10 - JavascriptVenu immadiView Answer on Stackoverflow
Solution 11 - JavascriptlowtechsunView Answer on Stackoverflow