5

How do we do a hard reload/refresh in Chrome using Javascript?

window.location.reload(true);
location.reload(true);

didn't do it for me.

===========================

What I meant by 'didn't do it for me...'

The session cookies (ex. JSESSIONID) were not renewed specially the HttpOnly ones.

What I want to achieve...'

  1. I wanted to reload the page like it's the first time I accessed the URL.
  2. I wanted to simulate the steps below (which is like accessing the URL for the first time)
   - Open browser
   - Type URL
   - Hit Enter

I wonder if there is a more powerful Javascript command that reloads as if it is the first time.

4
  • Can't reproduce. What version of Chrome are you using? Commented Sep 22, 2021 at 23:44
  • We need more details. How did you test this? stackoverflow.com/questions/2099201/… Commented Sep 22, 2021 at 23:47
  • 1
    the param is deprecated, if your trying to clear cache you should look into using cache busting vars on asset urls so if a file changes so does the param which will cause the file to freshly loaded, i.e version your files or use a service worker like workbox Commented Sep 22, 2021 at 23:54
  • I added more details. Hope you are getting my point. Commented Sep 23, 2021 at 0:43

2 Answers 2

2

The best (and practically only) way to ensure a page is hard reloaded is by using your server. One of the ways you can do this is by serving headers, when a page is requested, that invalidate resources such as Cache-Control which will tell the browser to not cache resources and always revalidate resources which means everything must be redownloaded each time.

I would not recommend serving this header on every request in your production application, though.

Cache-Control: no-store, max-age=0

On your page you can add meta tags that will tell the browser what to do, provided there weren't headers already passed to the browser. This depends on how you serve your content, but you can add the following to your head element which will tell the browser to store absolutely nothing and is equivalent to hitting a page for the first time:

<head>
...
<meta http-equiv="Cache-Control" content="no-store, max-age=0">
...
</head>
Sign up to request clarification or add additional context in comments.

9 Comments

As much as possible, I wouldn't like to go to server side
manually, I could just close the browser, open it back, and access the same URL again, then sessions cookies are refreshed. I literally would want to achieve this in Javascript
love to upvote your answer @Marcus Parsons, but as you can see, I can't vote yet. maybe my question is misleading but my comment above is actually what I wanted to achieve
It's okay if you can't upvote. I re-read your question after your edits, and I believe that my edited answer should help you out. Try adding that element to the head of your document. But your mileage may very because meta tag results depend upon how your content is served to the end user.
I'm in SI team so I don't really have the luxury to pass things back to the server side. I am just doing what I can with what I receive in the front-end.
|
0

location.reload() should work. Works for me!

3 Comments

location.reload() just reloads the current page. It doesn't perform a hard reload. In now obsolete methods of location.reload you could pass in true which would perform a hard reload. But, they are now obsolete, of course.
@The_Paradox, I tried this but it didn't reload the page like it's the first time it is accessing the URL
Not pulling data from server.

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.