31

I'm trying to refresh a page and then run a function once the refresh has been completed. However the code I have now, runs the function and then it only refreshes it, meaning I lose what the function did. Is there a way to solve this?

My code

function reloadP(){
    document.location.reload();
    myFunction();
}

<button onclick: "reloadP()">Click</button>    
Gass
  • 7,536
  • 3
  • 37
  • 41
Lost Soul
  • 345
  • 1
  • 3
  • 8

5 Answers5

50

You need to call myFunction() when the page is loaded.

window.onload = myFunction;

If you only want to run it when the page is reloaded, not when it's loaded for the first time, you could use sessionStorage to pass this information.

window.onload = function() {
    var reloading = sessionStorage.getItem("reloading");
    if (reloading) {
        sessionStorage.removeItem("reloading");
        myFunction();
    }
}

function reloadP() {
    sessionStorage.setItem("reloading", "true");
    document.location.reload();
}

DEMO

Barmar
  • 741,623
  • 53
  • 500
  • 612
5
function myFunction() {
    document.getElementById("welcome").textContent = "Welcome back!";
}

window.onload = function() {
    var reloading = sessionStorage.getItem("reloading");
    if (reloading) {
        sessionStorage.removeItem("reloading");
        myFunction();
    }
}

function reloadP() {
    sessionStorage.setItem("reloading", "true");
    document.location.reload();
}

DEMO: https://jsfiddle.net/barmar/5sL3hd74/

Félix Pujols
  • 107
  • 1
  • 7
1

Adding to @Barmar answer... In case you'd like to use session storage only when a button in the page is clicked and not when reloading with the browser button, you can use sessionStorage.clear() or sessionStorage.removeItem() once you've executed the function after loading the window.

So, let's say we have:

let restart = sessionStorage.getItem("restart")

Set restart boolean to true as a session storage and reload:

resetBtn.addEventListener("click", () => {
   sessionStorage.setItem("restart", "true")
   location.reload()
})

Once the window is reloaded we can execute the following function:

window.onload = () => {
  if(restart){
    // Do something
    sessionStorage.clear() // This cleans all the session storage
    
    // If you want to  remove ONLY the item from the storage use:
    // sessionStorage.removeItem("restart")
  }
};

So, if now the user reloads the page with the browser button it will reload with the session storage cleaned. Meaning, no functions will be executed after window load.

Gass
  • 7,536
  • 3
  • 37
  • 41
  • 1
    using `sessionStorage.clear()` does not seem to be the right choice. Because (as mentioned in the reply), it would not only erase our item but also clear other possibly stored items. So, using `sessionStorage.removeItem()` would be more appropriate – Syed Muhammad Sajjad Mar 07 '22 at 09:03
0

In my case i used Barmar's solution. I have a modal popup form, i want to submit the form then automatically refresh the page and finally success message on reloaded page.

var form = document.getElementById('EditUserInfosForm')
form.addEventListener('submit', function () {
    sessionStorage.setItem("reloading", "true");
    document.location.reload();
})

window.onload = function () {
    var reloading = sessionStorage.getItem("reloading");
    if (reloading) {
        sessionStorage.removeItem("reloading");
        $('#success-message-modal').modal('show')
    }
}
MrAlbino
  • 308
  • 2
  • 10
0

Probably simplest approach.

HTML Button

Reload button (credits):

<!-- index.html -->
<button onClick="window.location.reload();">Refresh Page</button>

JS Code

Run your code after reload:

// index.js
window.addEventListener("load", (event) => {
    YourFunction(); // already declared somewhere else
});

You may not use event variable at all.

https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event#examples

carloswm85
  • 1,396
  • 13
  • 23