Detect when a window is resized using JavaScript ?

Is there any way with jQuery or JavaScript to trigger a function when the user ends to resize the browser window?

In other terms:

  1. Can I detect mouse up event when user is resizing the browser window? Otherwise:
  2. Can I detect when a window resize operation is finished?

I'm currently only able to trigger an event when the user start to resize the window with jQuery



You can use .resize() to get every time the width/height actually changes, like this:

$(window).resize(function() {
  //resize just happened, pixels changed

You can view a working demo here, it takes the new height/width values and updates them in the page for you to see. Remember the event doesn't really start or end, it just "happens" when a resize occurs...there's nothing to say another one won't happen.

Edit: By comments it seems you want something like a "on-end" event, the solution you found does this, with a few exceptions (you can't distinguish between a mouse-up and a pause in a cross-browser way, the same for an end vs a pause). You can create that event though, to make it a bit cleaner, like this:

$(window).resize(function() {
    if(this.resizeTO) clearTimeout(this.resizeTO);
    this.resizeTO = setTimeout(function() {
    }, 500);

You could have this is a base file somewhere, whatever you want to do...then you can bind to that new resizeEnd event you're triggering, like this:

$(window).bind('resizeEnd', function() {
    //do something, window hasn't changed size in 500ms

You can see a working demo of this approach here ?


This can be achieved with the onresize property of the GlobalEventHandlers interface in JavaScript, by assigning a function to the onresize property, like so:

window.onresize = functionRef;

The following code snippet demonstrates this, by console logging the innerWidth and innerHeight of the window whenever it's resized. (The resize event fires after the window has been resized)

function resize() {
  console.log("height: ", window.innerHeight, "px");
  console.log("width: ", window.innerWidth, "px");

window.onresize = resize;
<p>In order for this code snippet to work as intended, you will need to either shrink your browser window down to the size of this code snippet, or fullscreen this code snippet and resize from there.</p>


Another way of doing this, using only JavaScript, would be this:

window.addEventListener('resize', functionName);

This fires every time the size changes, like the other answer.

functionName is the name of the function being executed when the window is resized (the brackets on the end aren't necessary).


If You want to check only when scroll ended, in Vanilla JS, You can come up with a solution like this:

window.onresize = resizeEnd;
var resizeEnded = false;
var timer, tolerance = 5; // 1 - 10
function resizeEnd() {
    timer = setTimeout(function() { 
        resizeEnded = true; 
        console.log('Resize ended at ' + window.innerWidth); 
        console.log('Resize ended at ' + window.innerHeight);
    }, tolerance * 100);

ES6 friendly.

window.onresize = resizeEnd
var resizeEnded = false
var timer, tolerance = 5 // 1 - 10
function resizeEnd() {
    timer = setTimeout(() => { 
        resizeEnded = true
        console.log('Resize ended at ' + window.innerWidth)
        console.log('Resize ended at ' + window.innerHeight)
    }, tolerance * 100);


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.