0

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?

3
  • 1
    You can't unless you control the page source itself. The JS context gets destroyed by the page load. Commented Oct 25, 2021 at 0:08
  • 1
    before to your reload you can use setTimeout to run a function some seconds after. Commented Oct 25, 2021 at 0:13
  • 1
    @JoseLora once the reload occurs the setTimeout will be cleared and no longer exists unfortunately. As for options, it depends on if you have access to a backend (or server side). Then you could have the ability to send a response and then then on the reload the server would be able to send a response with the value string. With only client-side, as @kshetline said, you would need a cookie to pass messages between reloaded pages. Commented Oct 25, 2021 at 0:25

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this made sense after your explanation. I ultimately got it working using cookies. I appreciate your input!
-1

Use extentions like TamperMonkey to handle your script.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.