Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh browser window using JS [duplicate]

Tags:

javascript

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?

like image 234
Kamran Ahmed Avatar asked Nov 05 '13 14:11

Kamran Ahmed


2 Answers

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

like image 190
MildlySerious Avatar answered Sep 19 '22 13:09

MildlySerious


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.

like image 43
Fokwa Best Avatar answered Sep 21 '22 13:09

Fokwa Best