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?
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?
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;
}
});
});
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.
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
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...
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();
});
});
$(window).resize( function() {
window.location.href = window.location.href;
});
This one worked for me!
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);
});
Just use this code
$(window).resize( function() {
window.location.href = window.location.href;
});
Hope it useful