10

I am trying to refresh the browser window using the following code:

window.location = window.location.href;

but it doesn't refresh the window, however when I try to do this:

window.location = "http://www.google.com";

It does redirect me to Google. Can anyone please tell me what I am doing wrong here? Why doesn't it refresh the browser window?

Kamran Ahmed
  • 11,809
  • 23
  • 69
  • 101

4 Answers4

31

Use window.location.reload(), it does exactly that.

MildlySerious
  • 8,750
  • 3
  • 28
  • 30
  • 5
    Also, I would use window.location.reload(true); so that the page is not refreshed from cache - it isn't needed always but sometimes it fixes issues :) – Kristijan Nov 05 '13 at 14:23
  • 3
    Just as a side note, the `window` object is actually applied implicitly in Javascript and is thus not needed as long as you don't define a new `location` object in local scope (i.e `location.reload()` will work just as well). – Daniel Perván Nov 05 '13 at 14:24
  • @Kristijan Judging from the docs, the boolean to trigger a noncached reload is not part of the spec. Or is it by now? Any info on the support for this? This sure is a nice thing to have. – MildlySerious Nov 05 '13 at 14:34
  • 1
    @MildlyInteresting You are right! :) The current spec doesn't specify this attribute but it is cross browser compatible (speaking about major browsers). Link to specification: http://www.whatwg.org/specs/web-apps/current-work/multipage/history.html#dom-location-reload – Kristijan Nov 07 '13 at 20:34
3

location.reload(forceGet) can do the trick. Also, you can set the parameter forceGet to true so that it reloads the page from the server and not from the browser cache. Doing this solve me some issues with IE11 and Edge. By default (location.reload()) will reload from the cache.

Read: http://www.w3schools.com/jsref/met_loc_reload.asp

In the past I did something like window.location = '' to refresh the current page but faced issues (wasn't getting the complete url) with Edge and IE 11 even though it worked well on all other major browsers. So I eventually used location.reload(true) to get to work on Edge.

Fokwa Best
  • 3,322
  • 6
  • 36
  • 51
1

you can even use location.reload();

vinay Maneti
  • 1,447
  • 1
  • 23
  • 31
1

In my experience the following is the best one

window.location.reload()

Also we can make it like,

window.location.href = window.location.href

the second one is getting the current location and assigning the location as current location :)

MSMOHAN
  • 235
  • 1
  • 11