-1

I want javascript/Jquery to execute a piece of code once the window is being resized. Is there any way to do this? I believe I read something like $(window).change (I could have written that so wrong but I'm learning) Though it misses any indicator that tells it when the window size changes.

Thanks in advance!!

2 Answers2

1

You can listen for the window.resize() function :

$(function(){
     $(window).resize(function(){
          // Your window was resized, do something here
     });
});

It's worth noting that this will be fired for any minor change, so if you are expecting it to occur frequently, you may want to employ an approach similar to this related discussion that uses a delay to detect when the resize has completed and then it will fire the event.

Community
  • 1
  • 1
Rion Williams
  • 74,820
  • 37
  • 200
  • 327
  • Thanks, this was what I was looking for, the webpage is really really light and only for me so having it giving the same command every millisecond is no issue, but I'll keep it in mind for any future project! – Lucas van Dongen Apr 19 '16 at 19:26
1

Your can use .resize() function.

Bind an event handler to the "resize" JavaScript event, or trigger that event on an element.

Use it like this:

$(document).ready(function(){
    $( window ).resize(function() {
          console.log("Fired!");
        });
  })
Dan Cantir
  • 2,915
  • 14
  • 24