11

Someone has listed 535 different ways to reload a page in Javascript:

http://www.phpied.com/files/location-location/location-location.html

For example:

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)
self.location.replace(location)
location['assign'](location)
window.location['assign'](location)
self['location'].assign(location)
self['location']['assign'](location)
self['location']['replace'](location)
location.href = location
location.href = self.location
location.href = window.location.href

I'm curious if anyone knows how these are treated differently on the browser - whether one refreshes the page but busts cache - or not as the case may be?

donohoe
  • 13,867
  • 4
  • 37
  • 59
  • 3
    it looks as though the majority of the ways involve: directly changing `location`, using `location.assign`, `location.replace` and `location.reload`. The rest are just duplicate ways of accessing the same values. – zzzzBov Aug 10 '11 at 17:14
  • 6
    More like 3 ways obfuscated 535 times – Alex Turpin Aug 10 '11 at 18:08
  • Use the standard window.location.reload() if you want it to work in all browsers. – Gerben Aug 10 '11 at 19:47
  • 1
    He forgot location=document.documentURI – Gerben Aug 15 '11 at 18:58
  • I stumbled upon this question while researching a chromium bug, and I can safely say that `location.reload(1);` and `window.location.replace(location);` don't run through the same codepath in that browser: https://bugs.chromium.org/p/chromium/issues/detail?id=280460#c11 Some answer here mentioned it may have to do with form [re]submission. – Vasiliy Sharapov Jun 01 '16 at 00:39

2 Answers2

7

All those examples are treated the same by the browser.

In fact, most of them are just different ways of accessing a variable in Javascript. You could probably write a similar list with "XXX different ways of assigning a global variable the value of another global variable".

For example: location.href, window.location.href, self.location.href, location['href'], window.location['href'], self.location['href'] are all pointing to the exact same value.

In the list you posted, the only two really different ways are these:

location = location //assign the whole location object to the location global variable
location = location.href //assign the string location.href to the location global variable
Felipe Brahm
  • 3,162
  • 1
  • 28
  • 42
  • In the examples he posted, none of the ways actually reload the page if the location has a hash. On the linked page are things like `location.reload()` though which reload the page regardless. – Robert Sep 14 '13 at 08:12
  • is there a way to reload the page without actually downloading it again? like a reset? – Zibri Dec 02 '19 at 11:15
6

location.reload() acts like a form submit (i.e. it passes all the form values)

Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
jdzakarian
  • 131
  • 2
  • 6