1

How can I save the foo variable (in this case a function) after the page gets reloaded? How can I call foo after the page was reloaded automatically?

var foo = function() {
  if(bar){
    location.reload();
    console.log('reload');
  }else{
    console.log('do something');
    //do something
  }
};
foo();
F. Ception
  • 25
  • 9

2 Answers2

1

I found a solution by myself using the chrome extension Resource Override. https://chrome.google.com/webstore/detail/resource-override/pkoacgokdfckfpndoffpifphamojphii

Just added the foo function to the head of the site and the function call to the body. Every time the side reloads, the code will be written inside the site (locally).

F. Ception
  • 25
  • 9
-1

You can't save the stuff that is evaluated in a console. Meaning everything that is evaluated in a console lives only during current page session. Once the page is reloaded - all the evaluation results are erased. In case if you still need to store this function - you might use localStorage or sessionStorage. But it is really redundant to store the function such a way... Why don't include this function directly to the html and be able to call it any time after the page is reloaded?

Dimas
  • 1
  • 1
  • Because I want to check a website for an update. I can't include to the script to the website. Is it possible to store the function in localStorage? – F. Ception Apr 19 '18 at 20:19
  • Yep, it is possible even though it is not recommended. It is well described here https://stackoverflow.com/questions/38926530/javascript-store-function-in-localstorage – Dimas Apr 19 '18 at 20:44