Updating one temporary page after loading the first page

I would like to implement JavaScript code that states: if the page is fully loaded, refresh the page immediately, but only once. I'm stuck on "just once"

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

this gives a loop without "just once." JQuery loads if this helps.

+74
javascript jquery refresh reload
Aug 08 '11 at 16:44
source share
15 answers

I would say using a hash, for example:

 window.onload = function() { if(!window.location.hash) { window.location = window.location + '#loaded'; window.location.reload(); } } 
+83
Aug 08 '11 at 16:53
source share

When I encounter this problem, I search here, but most of the answers try to modify the existing url. Here is another answer that 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> 
+35
Mar 03 '15 at 19:47
source share
 <script type="text/javascript"> $(document).ready(function(){ //Check if the current URL contains '#' if(document.URL.indexOf("#")==-1){ // Set the URL to whatever it was plus "#". url = document.URL+"#"; location = "#"; //Reload the page location.reload(true); } }); </script> 

Due to the if condition, the page will reload only once. I ran into this problem too, and when I searched, I found this nice solution. This works for me well.

+27
Apr 25 '14 at 11:09
source share

Check out this link. It contains java-script code that can be used to refresh the page only once.

http://www.hotscripts.com/forums/javascript/4460-how-do-i-have-page-automatically-refesh-only-once.html

There are several ways to refresh the page:

solution1

To refresh the page every time it opens, use:

 <head> <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <META HTTP-EQUIV="Expires" CONTENT="-1"> </head> 

sollution2:

 <script language=" JavaScript" > <!-- function LoadOnce() { window.location.reload(); } //--> </script> 

Then change your mind

 <Body onLoad=" LoadOnce()" > 

solution3:

response.setIntHeader("Refresh", 1);

But this solution will refresh the page more than once depending on the time you specify

I hope this helps you

+8
Sep 20 '13 at 12:32
source share

I put this in my title tags of the page on which I want to reload:

 <?php if(!isset($_GET['mc'])) { echo '<meta http-equiv="refresh" content= "0;URL=?mc=mobile" />'; } ?> 

the value of "mc" can be set to whatever you want, but both must match on two lines. and "= mobile" can be "= anythingyouwant" it just needs a value to stop the update.

+3
Jul 01 '12 at 20:19
source share

After the </body> :

 <script type="text/javascript"> if (location.href.indexOf('reload')==-1) { location.href=location.href+'?reload'; } </script> 
+1
Jul 28 '17 at 3:08 on
source share

You will need to use the GET or POST information. GET will be easier. Your JS will check the URL, if some parameter is not found, it will not just refresh the page, but rather send the user to a "different" URL, which will have the same URL, but with the GET parameter in it.

For example:
http://example.com β†’ will be updated
http://example.com?refresh=no β†’ not updated

If you do not need a messy URL, I would include some PHP at the beginning of the body that echoes a value that essentially says whether the POST option was enabled to not refresh the page in the initial page request. Immediately after this, you should enable JS to check this value and refresh the page with this POST information, if necessary.

0
Aug 08 2018-11-18T00:
source share

Try with this

 var element = document.getElementById('position'); element.scrollIntoView(true);` 
0
Feb 08 '13 at 10:18
source share

Please try with the code below.

 var windowWidth = $(window).width(); $(window).resize(function() { if(windowWidth != $(window).width()){ location.reload(); return; } }); 
0
Aug 12 '13 at 14:29
source share
  var foo = true; if (foo){ window.location.reload(true); foo = false; } 
0
Feb 10 '16 at 13:26
source share

use this

 <body onload = "if (location.search.length < 1){window.location.reload()}"> 
0
Jul 28 '16 at 10:12
source share

use window.localStorage ... like this

 var refresh = $window.localStorage.getItem('refresh'); console.log(refresh); if (refresh===null){ window.location.reload(); $window.localStorage.setItem('refresh', "1"); } 

It works for me

0
Apr 08 '17 at 16:37 on
source share

Finally, I got a solution to reload the page after two months of research.

It works great on my JS client project.

I wrote a function that below reloads the page only once.

1) First time browser downloads

2) Get the current timestamp

3) Browser loading time + 10 seconds

4) If the browser loading time is 10 seconds longer than the current timestamp, the page can be refreshed using "reloadPage ();"

But if it does not exceed 10 seconds, it means that the page just reloads, so it will not reload again.

5) Therefore, if you call "reloadPage ();" A function somewhere on your js file page will be reloaded only once.

Hope this helps someone

 // Reload Page Function // function reloadPage() { var currentDocumentTimestamp = new Date(performance.timing.domLoading).getTime(); // Current Time // var now = Date.now(); // Total Process Lenght as Minutes // var tenSec = 10 * 1000; // End Time of Process // var plusTenSec = currentDocumentTimestamp + tenSec; if (now > plusTenSec) { location.reload(); } } // You can call it in somewhere // reloadPage(); 
0
May 24 '18 at 14:11
source share

Here is another solution with setTimeout, not perfect, but it works:

This requires a parameter in the current URL, so just displaying the current URL looks like this:

 www.google.com?time=1 

The following code forces the page to reload only once:

  // Reload Page Function // // get the time parameter // let parameter = new URLSearchParams(window.location.search); let time = parameter.get("time"); console.log(time)//1 let timeId; if (time == 1) { // reload the page after 0 ms // timeId = setTimeout(() => { window.location.reload();// }, 0); // change the time parameter to 0 // let currentUrl = new URL(window.location.href); let param = new URLSearchParams(currentUrl.search); param.set("time", 0); // replace the time parameter in url to 0; now it is 0 not 1 // window.history.replaceState({}, "", '${currentUrl.pathname}?${param}'); // cancel the setTimeout function after 0 ms // let currentTime = Date.now(); if (Date.now() - currentTime > 0) { clearTimeout(timeId); } } 

The accepted answer uses the least amount of code and is easy to understand. I just provided another solution for this.

Hope this helps others.

0
Apr 10 '19 at 2:03
source share

Use rel = "external" as shown below,

 <li><a href="index.html" rel="external" data-theme="c">Home</a></li> 
-2
Feb 11 '16 at 11:42 on
source share



All Articles