255

How can I refresh a page using JavaScript or HTML?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Prabakaran
  • 2,585
  • 2
  • 15
  • 3
  • 25
    First entire PAGE of google with your question gives you the answer. – stefan Mar 14 '11 at 04:50
  • 5
    question like this should be closed. users should be encouraged to do such a simple search first, encourage to read.. – amosrivera Mar 14 '11 at 04:51
  • 35
    @amosrivera - Indicated by the ratio of upvotes to downvotes on this question, there is a silent majority of users who very much appreciate being able to find answers to questions like this on Stackoverflow – Chris Dutrow Apr 05 '12 at 12:41
  • 48
    @stefan: Thankfully, Google brought me (and thousands of others) here -- to find the *best* answers. – Brent Bradburn Aug 13 '14 at 04:12
  • 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. – Brick Dec 07 '16 at 17:21
  • 11
    Sadly, when you googgle for "auto refreshing a page via js" this one is the first page which appears, so not answering here is kinda pointless :) – Alar Jan 29 '18 at 12:34
  • @stefan Aaaand take a guess what is right up there at the top of that page... – shawnseanshaun Apr 10 '18 at 18:54

8 Answers8

350

window.location.reload(); in JavaScript

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

Reid
  • 18,959
  • 5
  • 37
  • 37
285

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

epoch
  • 16,396
  • 4
  • 43
  • 71
87

simply use..

location.reload(true/false);

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

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
Wolv3rine
  • 911
  • 7
  • 8
  • 19
    And default value is `false` for reload page from the cache. – Nabi K.A.Z. Nov 19 '17 at 15:13
  • 4
    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... – Canelo Digital Jan 13 '18 at 04:53
  • 2
    Now deprecated. Use `window.location.reload()` instead. – TD1 Mar 30 '21 at 16:43
  • 2
    NOTE: this will resubmit a form – Elia Weiss May 10 '21 at 06:05
17
window.location.reload()

should work however there are many different options like:

window.location.href=window.location.href
Mike Lewis
  • 63,433
  • 20
  • 141
  • 111
13

You can also use

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

It works fine for me.

Mandeep Singh
  • 1,287
  • 14
  • 34
6

Use:

window.location.reload();
Rob
  • 10,004
  • 5
  • 61
  • 91
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.

Codebeat
  • 6,501
  • 6
  • 57
  • 99
-6

try this working fine

jQuery("body").load(window.location.href);
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
Muhammad Waqas
  • 1,140
  • 12
  • 20