265

How can I refresh a page using JavaScript or HTML?

1
  • 2
    This question allows for a pure HTML answer (no JS), and the answer that's current top voted here includes an HTML option. In that respect, this question is different than the one that's marked duplicate of. Commented Dec 7, 2016 at 17:21

8 Answers 8

358

window.location.reload(); in JavaScript

<meta http-equiv="refresh" content="1"> in HTML (where 1 = 1 second).

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

1 Comment

location = location;
290

Here are 535 ways to reload a page using javascript, very cool:

Here are the first 20:

location = location
location = location.href
location = window.location
location = self.location
location = window.location.href
location = self.location.href
location = location['href']
location = window['location']
location = window['location'].href
location = window['location']['href']
location = window.location['href']
location = self['location']
location = self['location'].href
location = self['location']['href']
location = self.location['href']
location.assign(location)
location.replace(location)
window.location.assign(location)
window.location.replace(location)
self.location.assign(location)

and the last 10:

self['location']['replace'](self.location['href'])
location.reload()
location['reload']()
window.location.reload()
window['location'].reload()
window.location['reload']()
window['location']['reload']()
self.location.reload()
self['location'].reload()
self.location['reload']()
self['location']['reload']()

Original Article

12 Comments

no, i did not test them all
All the ones setting window.location to a variable (not executing a function) won't refresh if your address bar has a #hashValue in it, it will simply move to that ID on the page if it exists.
alot of the entries in that list are simply the same thing with different syntax, such as [] vs . syntax for accessing object properties.
Wow, look at the webpages source code, the repeating list is repeated so long he just used javascript to generate all the possible permutations instead of writing out his own ideas of ways.
But missing window.history.go(0) ;)
|
86

simply use..

location.reload(true/false);

If false, the page will be reloaded from cache, else from the server.

4 Comments

And default value is false for reload page from the cache.
this answer made my day, or more precise: my night... had a problem with location.reload() and some modal windows which were stuck in a previous state. location.reload(true); was the solution, as the default is indeed false, as Nabi K.A.Z. states. thank you very much...
Now deprecated. Use window.location.reload() instead.
NOTE: this will resubmit a form
19
window.location.reload()

should work however there are many different options like:

window.location.href=window.location.href

1 Comment

"window.location.href=window.location.href" doesn't seem to work anymore
12

You can also use

<input type="button" value = "Refresh" onclick="history.go(0)" />

It works fine for me.

1 Comment

NOTE: this will resubmit a form
6

Use:

window.location.reload();

Comments

2

If it has something to do control updates on cached pages here I have a nice method how to do this.

  • add some javascript to the page that always get the versionnumber of the page as a string (ajax) at loading. for example: www.yoursite.com/page/about?getVer=1&__[date]
  • Compare it to the stored versionnumber (stored in cookie or localStorage) if user has visited the page once, otherwise store it directly.
  • If version is not the same as local version, refresh the page using window.location.reload(true)
  • You will see any changes made on the page.

This method requires at least one request even when no request is needed because it already exists in the local browser cache. But the overhead is less comparing to using no cache at all (to be sure the page will show the right updated content). This requires just a few bytes for each page request instead of all content for each page.

IMPORTANT: The version info request must be implemented on your server otherwise it will return the whole page.

Example of version string returned by www.yoursite.com/page/about?getVer=1&__[date]: skg2pl-v8kqb

To give you an example in code, here is a part of my library (I don't think you can use it but maybe it gives you some idea how to do it):

o.gCheckDocVersion = function() // Because of the hard caching method, check document for changes with ajax 
{
  var sUrl = o.getQuerylessUrl(window.location.href),
      sDocVer = o.gGetData( sUrl, false );

  o.ajaxRequest({ url:sUrl+'?getVer=1&'+o.uniqueId(), cache:0, dataType:'text' }, 
                function(sVer)
                {
                   if( typeof sVer == 'string' && sVer.length )
                   {
                     var bReload = (( typeof sDocVer == 'string' ) && sDocVer != sVer );
                     if( bReload || !sDocVer )
                      { 
                        o.gSetData( sUrl, sVer ); 
                        sDocVer = o.gGetData( sUrl, false );
                        if( bReload && ( typeof sDocVer != 'string' || sDocVer != sVer ))
                         { bReload = false; }
                      }
                     if( bReload )
                      {  // Hard refresh page contents
                        window.location.reload(true); }
                   }
                }, false, false );
};

If you are using version independent resources like javascript or css files, add versionnumbers (implemented with a url rewrite and not with a query because they mostly won't be cached). For example: www.yoursite.com/ver-01/about.js

For me, this method is working great, maybe it can help you too.

Comments

-6

try this working fine

jQuery("body").load(window.location.href);

3 Comments

why down vote mr down vote ?
Probably because the OP asked for a Javascript method and you provided a JQuery method
In addition to being super inefficient, it doesn't even refresh the page...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.