Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One time page refresh after first page load

I would like to implement a JavaScript code which states this: if the page is loaded completely, refresh the page immediately, but only once. I'm stuck at the "only once":

window.onload = function () {window.location.reload()} 

this gives a loop without the "only once". jQuery is loaded if this helps.

like image 786
Martin Avatar asked Aug 08 '11 16:08

Martin


People also ask

How do you make a page refresh every 5 seconds?

Open the web page that you want to automatically refresh at certain seconds of the interval. Then, click on the extension icon in your Chrome bar and select the interval time.

How do I make HTML refresh automatically?

Approach 1: One can auto refresh the webpage using the meta tag within the head element of your HTML using the http-equiv property. It is an inbuilt property with HTML 5. One can further add the time period of the refresh using the content attribute within the Meta tag.


2 Answers

I'd say use hash, like this:

window.onload = function() {     if(!window.location.hash) {         window.location = window.location + '#loaded';         window.location.reload();     } } 
like image 171
Cokegod Avatar answered Sep 20 '22 11:09

Cokegod


When I meet this problem, I search to here but most of answers are trying to modify existing url. Here is another answer which works for me using localStorage.

<script type='text/javascript'>  (function() {   if( window.localStorage )   {     if( !localStorage.getItem('firstLoad') )     {       localStorage['firstLoad'] = true;       window.location.reload();     }       else       localStorage.removeItem('firstLoad');   } })();  </script> 
like image 30
Haimei Avatar answered Sep 18 '22 11:09

Haimei