user782104
user782104

Reputation: 13545

Window resize fires twice in Chrome and FireFox, but only one in Safari

I used this code to test under the latest version of Chrome , FX and Safari. If I press the maximize button, it will fire twice in FX and Chrome, but only once in Safari. I am using Windows 7. Surprisingly, if I use Windows XP, Chrome will fire twice and FX, Safari fire once. Is it possible to guarantee it will fire twice? Thanks

$(window).resize(function() {

    if (!isiPhone()) {
        if ($(".viewportBinder").length){                   
            $("#view").css('height',$(window).height());
            $("#view").css('width',$(window).width());  
            content = element.viewport('update'); 
        }

        var h = screen.height;
        var w = screen.width;

        $(window).css("height",h);
        $(window).css("width",w);

        Book.book_position();
        Book.zoom_auto();

        /*
        var is_firefox = navigator.userAgent.indexOf('Firefox') > -1;
        var is_safari = navigator.userAgent.indexOf("Safari") > -1;

        if ((is_firefox)||(is_safari))*/
        alert ('test');
    }   

});

Upvotes: 0

Views: 2250

Answers (3)

Zafer Hangül
Zafer Hangül

Reputation: 76

I think, that's not a big problem. I solve the problem, usually using a method which is fired when resize finished.

var after_resize = (function(){
var timer = 0;
return function(callback, ms){
    clearTimeout (timer);
    timer = setTimeout(callback, ms);
};
})();
$(window).resize(function() {
after_resize(function(){
    alert("resize finish!");
}, 300);
});

Upvotes: 1

closure
closure

Reputation: 7452

This is not a problem. It browser dependent. You will need to handle this situation.

How about remembering your last action and ignoring it if the parameters are same.

You may code like:

function myResizer() {
    if (!isiPhone()) {
        if ($(".viewportBinder").length) {                   
            var height, width;
            height = $(window).height();
            width = $(window).width();
            if ((myResizer.height !== height) || (myResizer.width !== width)) {
                $("#view").css('height',$(window).height());
                $("#view").css('width',$(window).width());  
                content = element.viewport('update'); 
                myResizer.height = height;
                myResizer.width = width;
            }
        }

        var h = screen.height;
        var w = screen.width;

        if ((myResizer.h !== h) || (myResizer.w !== w)) {
            $(window).css("height",h);
            $(window).css("width",w);

            Book.book_position();
            Book.zoom_auto();
            myResizer.h = h;
            myResizer.w = w;
        }
    }   
}

$(window).resize(myResizer);

Upvotes: 1

algorhythm
algorhythm

Reputation: 8728

the resize event fires not ones, if you resize your browser... it fires multiple times, maybe for each pixel, don't know. it depends on the browser...

see also here: javascript resize event firing multiple times while dragging the resize handle

Upvotes: 1

Related Questions