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?

0

4 Answers 4

31

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

4
  • 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 5, 2013 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). Nov 5, 2013 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. Nov 5, 2013 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: whatwg.org/specs/web-apps/current-work/multipage/…
    – Kristijan
    Nov 7, 2013 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.

1
  • This question is 3 years old and you repeated the accepted answer.
    – Xatenev
    Aug 18, 2016 at 12:52
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 :)

1

you can even use location.reload();

1
  • No need to use a function for something that exists.
    – Kristijan
    Nov 5, 2013 at 14:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.