Page refresh function callback

Is there a way to implement a callback for the page refresh function, check if the page is ready after it is updated, and then print the message? I would like to show an updated page and show a specific message after clicking a button, however I need to stay within my code to find out which message to show. Anything like the following code would be great:

location.reload(function(){ alert ('done refreshing and document is ready'); }); 
+4
source share
3 answers

When you reload the page, your JS application is initialized again and restarted, so you cannot receive a callback.

However, what you can do is add the hash to the URL before reloading.

 window.location = window.location.href + "#refresh"; window.location.reload(); 

Then, when loading the page, check if a hash fragment exists. If this happens, you will know that you have just refreshed the page.

+11
source

In use

 $(document).ready(function(){ alert('done refreshing and document is ready'); }); 

But it does not distinguish the first download from the update. But you can achieve this using sessions. For example, using PHP:

 <?php session_start(); $showJs = false; if( !isset($_SESSION['loaded_before']) ) { $showJs = true; $_SESSION['loaded_before'] = true; } ?> ... <?php if($showjs) { ?> <script type="text/javascript"> $(document).ready(function(){ alert('done refreshing and document is ready'); }); </script> <?php } ?> 
0
source

If you really want to switch to the javascript side, you can use sessionStorage.varName before reloading, then check this variable and continue.

0
source

Source: https://habr.com/ru/post/1484497/


All Articles