Detect window change of resolution

2

I need when the user resizes the window, I can detect to be able to send the page to give a refresh.

    
asked by anonymous 29.10.2014 / 19:09

3 answers

6

In JavaScript we have an event handler for the resizing event of window , the window.onresize (English) .

window.onresize = function(){
   location.reload();
};

But attention:

Turning on a page refresh while changing the screen size is tricky, because while we are resizing, the page reloads, re-detects the change, reloads ...

The solution is to ensure that the size change is over and then call the update:

function recarregarPagina(){
    // Sem redimencionamento à 100ms!
    location.reload();
}

var boraLa;
window.onresize = function(){
  clearTimeout(boraLa);
  boraLa = setTimeout(recarregarPagina, 100);
};

Here, using setTimeout() (English ) and clearTimeout() (English) We are checking every 100 milliseconds if the page is still being resized. If it's over, we call our function recarregarPagina() .

While the user is resizing the screen, the timer is always set to 0 (zero). When the user stops resizing the screen, after 100 milliseconds our recarregarPagina() function is called. Inside it we have the code to execute, in your case: location.reload(); .

    
29.10.2014 / 19:44
3

If you can use JQuery:

$(window).resize(function() {
  // código que será executado quando o browser for redimensionado.
});

See: JQuery - .resize method

    
29.10.2014 / 19:14
0
//recarrega a pagina se mudar a resolução.
$( window ).resize(function() {
  document.location.reload();
});//
    
29.10.2014 / 19:15