6

I have a function that does a reload from server with:

location.reload(true)

Currently, any code I have after this line is not executed.

I'm curious if there is a way to be able to have more code run after the reload?

GMachado
  • 791
  • 10
  • 24
deal tek
  • 131
  • 1
  • 12
  • 2
    When you reload the page, the script also reloads, like a program restarting. Thus, the code doesn't run after it. You probably shouldn't reload if you want to do something after that. Can you give us some context? – clabe45 Sep 05 '18 at 20:07
  • Answer exists here https://stackoverflow.com/questions/41904975/refresh-page-and-run-function-after-javascript – CaseyC Sep 05 '18 at 20:10
  • I don't understand. What does your extra code do? Can't you execute the code first and THEN reload? – Damián Pablo González Sep 06 '18 at 00:50
  • Are you sure you even need the `location.reload(true)`? It's 2018 now, AJAX is well supported, no need to re-fetch the whole page when only some of the content has updated. – Kaiido Sep 06 '18 at 01:25
  • Related: [Does JavaScript reload() stop the remainder of the page from being processed?](/q/8411163/4642212), [Why is the content not showing after refresh](/q/57778178/4642212). – Sebastian Simon Oct 21 '22 at 22:39

3 Answers3

3

The best way to do this would be to write a function that is called if there are certain "GET" variables set and to check them on each run such as mypage.html?reload=true

Or even hash locations: mypage.html#reload

And you can compare like so:

$(document).ready(function () {
  if (window.location.hash == '#reload') {
    onReload();
  }
});

function onReload () {
  console.log('test');
}

function reload () {
  window.location.hash = 'reload';
  window.location.reload();
}

Just call the reload(); function.

Output (in console): test

And then you can just redirect people away from the hash #reload if you don't want your function to run if the page is refreshed again: window.location.hash = '';

NotoriousPyro
  • 526
  • 3
  • 16
  • So sorry for the very long delay getting back to you. I just learned about voting and comments and want to thank you for your efforts. – deal tek Mar 08 '20 at 16:30
  • This is a good solution when you need to pass some data after reload, just generate some random hash as a key for the location, and put the data under the same key to the sessionStorage... Then, after location.reload() take that hash from location, look into the sessionCache, do the necessary and cleanup the data from the cache and the location.hash... – Richard Toth Jan 24 '22 at 20:59
  • Thanks for the additional suggestion. – deal tek Sep 08 '22 at 22:20
1

why you need execute something after reload the page?

If you need to do something after reload, so you can pass some parameter to the page and check this to execute something additional. You can redirect to the same page and pass a complete url

var url = window.location.href; 
url += '&param=1'
window.location.href = url;

So, when your page reload, you can check a parameter by GET and do whatever you want. I hope this trick help you!

1

________________METHOD 1: WITH PHP_________________

1) reloading with a parameter

2) getting that parameter with PHP

For example:

window.location.href += '&reloading=1';
//it's equivalent to:
//window.location.href = window.location.href + '&reloading=1';

And then:

<?php
if (isset($_GET['reloading'])) {
    echo '<script>pageReloaded();</script>';
}
?>

Since PHP will be executed first, that little script (the function call) will be writen and then executed in javascript, so pageReloaded() function will be called.

A full example would be:

<?php if (isset($_GET['reloading'])) echo '<script>pageReloaded();</script>'; ?>
<script>
    function reloadNow(){
        alert("I'm about to reload");
        window.location.href += '&reloading=1';
    }
    function pageReloaded(){
        alert("I've been reloaded!");
    }
</script>

<button onclick="reloadNow()">Press to reload</button>

________________METHOD 2: NO PHP, ONLY JAVASCRIPT_________________

Using SessionStorage

1) Set SessionStorage.

2) Reload

3) Get that SessionStorage

For example:

<script>
    function reloadNow(){
        alert("I'm about to reload");
        sessionStorage.setItem('reloading',"true");
        window.location.reload(true);
    }

    //when loading:        
    if (sessionStorage.getItem('reloading') === "true") {
        sessionStorage.clear(); //so it doesn't trigger next time
        alert("I've been reloaded!");
    }
</script>

<button onclick="reloadNow()">Press to reload</button>
  • Php is not needed for this, it can be handled purely by javascript and a server side call is extremely taxing. – NotoriousPyro Sep 06 '18 at 01:26
  • 2
    @NotoriousPyro OP is doing `location.reload(true)` so the call to server-side is anyway done. – Kaiido Sep 06 '18 at 01:27
  • No, the difference between a php (a dynamic page) and a static js file are massive. Php MUST be reloaded from server, js can be loaded from client cache. – NotoriousPyro Sep 06 '18 at 01:47
  • If OP was calling a php file for every client vs each client managing their own "reload" sensing via js, only one solution out of these two is scalable. – NotoriousPyro Sep 06 '18 at 01:52
  • Guys, i'm giving AN OPTION here. A valid and working option. Since OP hasn't specify "only server-side", there's nothing wrong with it (and I really don't think it deserved a down vote!). – Damián Pablo González Sep 06 '18 at 01:59
  • 1
    @NotoriousPyro. I edited the answer to include an only-client-side solution – Damián Pablo González Sep 06 '18 at 02:34
  • 3
    THANK YOU SO MUCH for these great responses. I will check into them right away! – deal tek Sep 06 '18 at 18:00
  • These are all great ideas - thanks so much! In my case after "location.reload(true)" some page items were not showing correctly. I did have a function that updated the page so in my case I was able to add the function inside the "$(document).ready(function(){}" and this did the trick. Thanks again dave – deal tek Sep 08 '18 at 23:07
  • @dealtek, great! I'm glad I could help. Please don't forget to vote as best answer. – Damián Pablo González Sep 09 '18 at 20:11