Cross-browser window resize event - JavaScript / jQuery

269,159

Solution 1

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

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

    alert('resize');

});

Solution 3

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

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

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

Share:
269,159

Related videos on Youtube

BuddyJoe
Author by

BuddyJoe

I like to code C# and work with the web. Still learning.

Updated on October 07, 2020

Comments

  • BuddyJoe
    BuddyJoe over 3 years

    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?

    • Byff
      Byff over 13 years
      I'm also looking into this. I'd like to try to apply the solutions offered here, but I'm afraid I don't understand the syntax: $(window). Is that something specific to jquery?
    • Harshit Shah
      Harshit Shah over 13 years
      Byff, yes, $(window) is a jQuery construct. window.onresize is the equivalent in raw JavaScript.
    • Subodh Ghulaxe
      Subodh Ghulaxe about 11 years
      This demo may help anybody to test on all browsers jsfiddle.net/subodhghulaxe/bHQV5/2
    • Dylan Valade
      Dylan Valade about 8 years
      window.dispatchEvent(new Event('resize')); stackoverflow.com/questions/1818474/…
    • psur
      psur almost 6 years
      On Opera on mobile it is triggered each time top bar (browser UI) is showing/hiding.
  • Sly
    Sly almost 13 years
    Why this event is not fired when window is expanded to full screen with button?
  • Mike Kormendy
    Mike Kormendy almost 12 years
    I prefer the bind approach, since the resize() method is actually actually a shortcut for the full bind approach, and I often find that there is usually more than one event handler I should be applying to an element.
  • Mark Vital
    Mark Vital over 11 years
    @Sly For me it works,in Safari, Chrome and Mozilla on Mac. Which browser do you use?
  • Zack Zatkin-Gold
    Zack Zatkin-Gold over 11 years
    @Sly: $('.button').on({ click: function(){ /* your code */ $(window).trigger('resize') })
  • Elijah Lynn
    Elijah Lynn over 11 years
    Also of note is that if you call a named function just type the name, don't quote it or put parentheses if there are no arguments to pass to it.)
  • Yoh Suzuki
    Yoh Suzuki over 11 years
    Elijah, this is JavaScript. What you wrote is not correct (except for one specific case when you are constructing with new).
  • Dan
    Dan over 11 years
    Considering most need the optimized version for UI responsiveness, it would be nice if jQuery added a second parameter, being a delay (in millis) which would automatically add the 'responsiveness' logic as proposed above.
  • Shane
    Shane about 11 years
    @Mike - Not only that, but I'm guessing the resize method has no "unbind" method in the event you want to remove the listener. "bind" is definitely the way to go!
  • Tom Auger
    Tom Auger almost 11 years
    Just be aware that this will clobber any existing handlers that may be bound to window.onresize. If your script has to live an an ecosystem with other scripts, you shouldn't assume your script is the only one to want to do something on window resize. If you can't use a framework such as jQuery, you should at least consider grabbing the existing window.onresize, and adding your handler to the end of it, rather than completely clobbering it.
  • Tomas
    Tomas over 10 years
    @TomAuger "you should at least consider grabbing the existing window.onresize, and adding your handler to the end of it" - you probably meant the opposite, i.e. "to call existing handler at the end of your handler"? I would really like to see a javascript code that would add something to the end of already existing function :-)
  • Tom Auger
    Tom Auger over 10 years
    I see your point, in a sense - your new onResize handler needs to wrap the existing onResize. However, it doesn't have to call the function you wanted to add to onResize until after it has called the original onResize handler. So you can still make sure your new onResize function executes after the original onResize function, which is probably better than the reverse.
  • Jondlm
    Jondlm about 10 years
    See my answer below if you don't want to use jQuery.
  • WraithKenny
    WraithKenny about 10 years
    There's two changes. Which one had the effect? (.resize() to .bind('resize') or adding the anonymous function? I'm thinking it's the second.)
  • bassim
    bassim about 10 years
    @WraithKenny Good point. It very well may be that adding the anonymous function was what made it work. I don't recall if I tried it.
  • Alexandru Trandafir Catalin
    Alexandru Trandafir Catalin about 9 years
    I had a issue in Firefox saying that $ is not defined when the resize event was fired. I've put my resize function inside a setTimeout and it worked now!
  • RTF
    RTF about 9 years
    It's worth mentioning that this jQuery function "adds" a function call to the current list of functions that should be called on a window resize. It does not overwrite existing events that are currently scheduled on a window resize.
  • Sergey
    Sergey over 8 years
    this solution wouldn't be working in IE. Fixed version with support of IE dom: stackoverflow.com/questions/6927637/…
  • josinalvo
    josinalvo almost 7 years
    > It does not throttle anything for you. Could you please elaborate? What should/would it throttle? Does it relate to the event firing many times in a row?
  • Jondlm
    Jondlm almost 7 years
    @josinalvo generally when you're listening to an event that fires a lot you'll want to debounce (e.g. lodash) it so you don't cause a bottleneck in your program.
  • Henry Blyth
    Henry Blyth almost 7 years
    In the first case, the adjustSize method loses its context of this, whereas the second call CmsContent.adjustSize() preserves this, i.e. this === CmsContent. If the CmsContent instance is required in the adjustSize method, then the first case will fail. Second case will work for every kind of function call, so it's recommended to always pass an anonymous function.
  • bassim
    bassim over 6 years
    @HenryBlyth Thank you! +1