21

Is it possible to refresh a page on browser size change? I use some styles that create areas on a page and if the browser is scaled down the layout break.

Perhaps I can detect the document size change with jQuery?

Matt
  • 74,352
  • 26
  • 153
  • 180
santa
  • 12,234
  • 49
  • 155
  • 255
  • magic jQuery..., or maybe with a DOM event? – Jan Apr 29 '11 at 20:04
  • 2
    When you say "if the browser is scaled down" do you mean the user resizes the browser window OR the user zooms out (Ctrl- or Cmd-)? Totally different situations. – RwwL Apr 29 '11 at 20:23
  • #Check my answer : Its perfect and tested: http://stackoverflow.com/questions/599288/cross-browser-window-resize-event-javascript-jquery/17059269#17059269 – Abhishek Goel Jun 12 '13 at 06:52

9 Answers9

28

Update for anyone viewing this now. JQuery now considers bind a deprecated function.

And the way proximus' response works (at least in Opera/Chrome/Firefox) it constantly polls for resizing even if the browser is just sitting there. It appears that the resize function was called automatically when it hit location.reload(), causing it to hit an infinite loop. Here's what I pulled together that also solved the problem.

jQuery(function($){
  var windowWidth = $(window).width();
  var windowHeight = $(window).height();

  $(window).resize(function() {
    if(windowWidth != $(window).width() || windowHeight != $(window).height()) {
      location.reload();
      return;
    }
  });
});
JR Smith
  • 1,186
  • 15
  • 20
27

Here's a pure JS solution, because I think it's not fair to use jQuery for everything.

window.addEventListener('resize', function () { 
    "use strict";
    window.location.reload(); 
});

A drawback is that it won't work in IE "browser" older than 9.

David Archibald
  • 1,431
  • 14
  • 27
uKolka
  • 36,422
  • 4
  • 33
  • 44
5

Simple. This doesn't fire multiple events just one event every x second(s). The accepted answer will throw too many events

var resizeTimeout;
window.addEventListener('resize', function(event) {
  clearTimeout(resizeTimeout);
  resizeTimeout = setTimeout(function(){
    window.location.reload();
  }, 1500);
});

for jQuery replace window.addEventListener('resize' with $(window).resize

aWebDeveloper
  • 36,687
  • 39
  • 170
  • 242
  • 2
    This is an important concern if you want the user to be able to drag the corner of the window, and deserves to be higher up. – Adam Chalcraft Feb 13 '20 at 02:04
5

If you're looking for a jQuery solution, you can use something like this:

$(window).bind('resize', function() {
     location.reload();
});

Maybe it makes sense to additionally use a timeout...

proximus
  • 689
  • 7
  • 20
  • 1
    In this case, it doesn't really matter because it'll just reload the page the first time resize is called. Use a timeout when you're actually running code on resize that might be repeated many hundreds of times by WebKit or IE, who fire that event a lot while the window is being resized. – RwwL Apr 29 '11 at 20:21
3

The already mentioned solutions work for the most of the cases. Safari on mobile devices (like tablets or phones) slightly changes the resolution when a user starts scrolling. This yields to an unusable page because the site refresehes when the user starts scrolling. To avoid this, set a threshold in px. If the browser resizes above this threshold, the page will reload. A jQuery solution might look like this:

var w = $(window).width();
var h = $(window).height();
var threshold = 100
$(window).resize(function(){
    widthDiff = Math.abs($(window).width()-w);
    heightDiff = Math.abs($(window).height()-h);
    if (widthDiff < threshold && heightDiff < threshold) return;
    w = $(window).width();
    h = $(window).height();

    $(window).resize(function(){
        location.reload();
    });
});
mhellmeier
  • 1,982
  • 1
  • 22
  • 35
2
$(window).resize( function() {

window.location.href = window.location.href;

});

This one worked for me!

Luke
  • 21
  • 1
2

There is a problem if someone drag window to resize - there would be too many resize events.

It is better to use timer, to clean that up:

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

    if (timeoutId) {
        clearTimeout(timeoutId);
    }

    timeoutId = setTimeout(function () {
        location.reload();
    }, 500);
});
Dampas
  • 323
  • 2
  • 6
  • That is a problem everyone is ignoring here! Your code needs a `var timeoutId = false;` before the first line though. – Hafenkranich Feb 27 '23 at 11:05
1

Just use this code

$(window).resize( function() {

window.location.href = window.location.href;

});

Hope it useful

Sakata Gintoki
  • 1,817
  • 16
  • 23
1

There is an onresize event you can listen for:

https://developer.mozilla.org/en/DOM/element.onresize

DA.
  • 39,848
  • 49
  • 150
  • 213