How to refresh the page and stop updating?

Asked

Viewed 33 times

0

I’m making the following code:

    if(xhr.status == 200){

      console.log(xhr.responseText);
      window.location.reload();

    }

only when I do this, it feels like it enters an eternal loop and crashes my server. How do I make it refresh the page just once and stop?

  • Yes, because the loop is getting in the way

  • The responseText is only to show me the data I’m receiving. I don’t need to perform any function. I’ll even take it out of the code later..

1 answer

1


You can use the document.referrer and compare with the current URL: if it’s different run Reload, if not, it does nothing:

if(document.referrer != location.href){   
   window.location.reload();
}

Problem: if the page is updated with F5 or the browser update button, referrer takes the current URL done by .reload() and the window.location.reload(); will not be executed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.