使用JavaScript重新加载页面的535种方法 - 后果是什么?

时间:2011-08-10 17:01:30

标签: javascript reload

有人列出了535种在Javascript中重新加载页面的方法:

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

例如:

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

我很好奇是否有人知道如何在浏览器上区别对待这些内容 - 无论是刷新页面还是刷新缓存 - 或者不是视情况而定?

2 个答案:

答案 0 :(得分:6)

浏览器会对所有这些示例进行相同的处理。

实际上,它们中的大多数只是在Javascript中访问变量的不同方式。您可以编写一个类似的列表,其中包含“XXX为全局变量分配另一个全局变量值的不同方法”。

例如:location.href, window.location.href, self.location.href, location['href'], window.location['href'], self.location['href']都指向完全相同的值。

在您发布的列表中,只有两种不同的方式是

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

答案 1 :(得分:5)

location.reload()就像表单提交一样(即它传递了所有表单值)