Elim Garak
Elim Garak

Reputation: 1817

window.onresize trapped only once

I have a jQuery plugin I added to a site and it handled window.onresize but never fired.

I then noticed that the page of the site had window.onresize on it already. Looking at this further it seems only the last window.onresize get fired.

For example if you run this script the only one that will be logged is Resize3.

Is there any way to trapped this event if it was handled somewhere else in a site?

$(function() {
 window.onresize = function () {
    console.log("Resize1");};

window.onresize = function () {
    console.log("Resize2");};

window.onresize = function () {
     console.log("Resize3");};
}); 

Upvotes: 0

Views: 56

Answers (1)

Bradley4
Bradley4

Reputation: 500

window.onresize is plain javascript, and as the pointed out in a comment, it will just get overwritten. With jQuery, you can declare it multiple times though, like this:

$(window).on('resize',function(){
     console.log("Resize1");
});

$(window).on('resize',function(){
     console.log("Resize2");
});

$(window).on('resize',function(){
     console.log("Resize3");
});

Just a note though, it might be better to have a single onresize function, which calls multiple functions inside of it, like this:

$(window).on('resize',function(){
     functionOne();
     functionTwo();
     functionThree();
});

Upvotes: 1

Related Questions