How do I run additional JS function after calling location.reload()?

I am trying to run some code that refreshes the webpage, and THEN does the console log.

The issue is the console.log is happening before the page reload.

location.reload();
console.log("test");

How do I adjust this to get the desired behavior?

Answer

The very act of refreshing a web page ends execution of scripts on that page, apart from what might be called in onbeforeunload or onunload handlers. Once the reload has actually occurred, no more JavaScript in the pre-reload page can be executed.

If you need to cause something to happen after content has been reloaded, you’ll have to use something like a cookie to pass a message to the reloaded page indicating the action you want to take.