-2

I just want to make a very common thing: Letting a website reload when browser window is resized. But the function behaves very weird.

After having tried the javascript possibility, I switched to jQuery. Without success.

I tried these two versions, one with "reload" and one with "reload()".

This one just triggers the alert without doing anything:

$(window).resize(reload);


function reload() {
    document.location.reload(true);
    alert("here");
}

And that one just endlessly reloads all the time:

$(window).resize(reload());


function reload() {
    document.location.reload(true);
    alert("here");
}

The endless reload also occurs when using a javascript eventlistener.

Johnny
  • 41
  • 3
  • `$(window).resize(reload());` is calling reload and assigns what it returns to the event handler..... So it is working as designed. – epascarello Apr 02 '19 at 18:52
  • 3
    *"make a common thing"* ... A reload in resize handler is not at all common....and sounds like a bad idea unless you throttled it and wait for resizing to stabilize – charlietfl Apr 02 '19 at 18:52
  • The loop that you see is due to the reload being triggered multiple times by the resize... you have to trigger it only if the resize is done This [Question](https://stackoverflow.com/questions/5489946/jquery-how-to-wait-for-the-end-of-resize-event-and-only-then-perform-an-ac) may help. – DIEGO CARRASCAL Apr 02 '19 at 19:01
  • 2
    `responsive design` != reload on resize, fwiw – Taplar Apr 02 '19 at 19:06
  • Why do I get an alert with the first method? Isn't this an incorrect way of calling the function and therefore nothing sholud happen? – Johnny Apr 02 '19 at 19:23

1 Answers1

0
<body onresize="reload()">    
<p>A</p>
<script>
    var W = window.outerWidth;
    var H = window.outerHeight;

    function reload() {

        var w = window.outerWidth;
        var h = window.outerHeight;

        if( W != w || H != h ){
            alert('Here');
            window.location.href = window.location.href;
        }
    }
</script>
</body>