Как я могу перезагрузить страницу, используя JavaScript?
Мне нужен метод, который работает во всех браузерах.
Как я могу перезагрузить страницу, используя JavaScript?
Мне нужен метод, который работает во всех браузерах.
JavaScript 1.0
window.location.href = window.location.pathname + window.location.search + window.location.hash;
// creates a history entry
JavaScript 1.1
window.location.replace(window.location.pathname + window.location.search + window.location.hash);
// does not create a history entry
JavaScript 1.2
window.location.reload(false);
// If we needed to pull the document from
// the web-server again (such as where the document contents
// change dynamically) we would pass the argument as 'true'.
location.reload();
Подробнее об объекте location
см. в странице MDC.
Вот 535 способов перезагрузить страницу, используя JavaScript, самый простой из которых - location = location
.
Это первые 50:
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)
location['replace'](location)
window.location['assign'](location)
window.location['replace'](location)
window['location'].assign(location)
window['location'].replace(location)
window['location']['assign'](location)
window['location']['replace'](location)
self.location['assign'](location)
self.location['replace'](location)
self['location'].assign(location)
self['location'].replace(location)
self['location']['assign'](location)
self['location']['replace'](location)
location.href = location
location.href = location.href
location.href = window.location
location.href = self.location
location.href = window.location.href
location.href = self.location.href
location.href = location['href']
location.href = window['location']
location.href = window['location'].href
location.href = window['location']['href']
location.href = window.location['href']
location.href = self['location']
location.href = self['location'].href
location.href = self['location']['href']
location.href = self.location['href']
...
Вы можете выполнить эту задачу, используя window.location.reload();
, Поскольку есть много способов сделать это, но я думаю, что это подходящий способ перезагрузить тот же документ с помощью JavaScript. Вот объяснение
JavaScript window.location
объект может быть использован
window
: в JavaScript представляет открытое окно в браузере.
location
: в JavaScript хранится информация о текущем URL.
Объект location
похож на фрагмент объекта window
и вызывается через свойство window.location
.
location
объекта имеет три метода:
assign()
: используется для загрузки нового документаreload()
: используется для перезагрузки текущего документаreplace()
: используется для замены текущего документа новым Поэтому здесь нам нужно использовать reload()
, потому что это может помочь нам перезагрузить тот же документ.
Так что используйте его как window.location.reload();
,
Чтобы попросить браузер извлечь страницу непосредственно с сервера, а не из кэша, вы можете передать true
параметр в location.reload()
. Этот метод совместим со всеми основными браузерами, включая IE, Chrome, Firefox, Safari, Opera.
Пытаться:
window.location.reload(true);
Параметр, установленный на true, перезагружает свежую копию с сервера. Оставив его, вы получите страницу из кэша.
Дополнительную информацию можно найти на MSDN и в документации Mozilla.
Это работает для меня:
function refresh() {
setTimeout(function () {
location.reload()
}, 100);
}
Я искал некоторую информацию о перезагрузках на страницах, полученных с помощью запросов POST, например, после отправки формы method="post"
.
Чтобы перезагрузить страницу , сохраняя данные POST, используйте:
window.location.reload();
Чтобы перезагрузить страницу отбросить данные POST (выполнить запрос GET), использовать:
window.location.href = window.location.href;
Надеюсь, это поможет другим людям найти ту же информацию.
500 других способов перезагрузки страницы с помощью JavaScript
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)
location['replace'](location)
window.location['assign'](location)
window.location['replace'](location)
window['location'].assign(location)
window['location'].replace(location)
window['location']['assign'](location)
window['location']['replace'](location)
self.location['assign'](location)
self.location['replace'](location)
self['location'].assign(location)
self['location'].replace(location)
self['location']['assign'](location)
self['location']['replace'](location)
location.href = location
location.href = location.href
location.href = window.location
location.href = self.location
location.href = window.location.href
location.href = self.location.href
location.href = location['href']
location.href = window['location']
location.href = window['location'].href
location.href = window['location']['href']
location.href = window.location['href']
location.href = self['location']
location.href = self['location'].href
location.href = self['location']['href']
location.href = self.location['href']
location.assign(location.href)
location.replace(location.href)
window.location.assign(location.href)
window.location.replace(location.href)
self.location.assign(location.href)
self.location.replace(location.href)
location['assign'](location.href)
location['replace'](location.href)
window.location['assign'](location.href)
window.location['replace'](location.href)
window['location'].assign(location.href)
window['location'].replace(location.href)
window['location']['assign'](location.href)
window['location']['replace'](location.href)
self.location['assign'](location.href)
self.location['replace'](location.href)
self['location'].assign(location.href)
self['location'].replace(location.href)
self['location']['assign'](location.href)
self['location']['replace'](location.href)
window.location = location
window.location = location.href
window.location = window.location
window.location = self.location
window.location = window.location.href
window.location = self.location.href
window.location = location['href']
window.location = window['location']
window.location = window['location'].href
window.location = window['location']['href']
window.location = window.location['href']
window.location = self['location']
window.location = self['location'].href
window.location = self['location']['href']
window.location = self.location['href']
location.assign(window.location)
location.replace(window.location)
window.location.assign(window.location)
window.location.replace(window.location)
self.location.assign(window.location)
self.location.replace(window.location)
location['assign'](window.location)
location['replace'](window.location)
window.location['assign'](window.location)
window.location['replace'](window.location)
window['location'].assign(window.location)
window['location'].replace(window.location)
window['location']['assign'](window.location)
window['location']['replace'](window.location)
self.location['assign'](window.location)
self.location['replace'](window.location)
self['location'].assign(window.location)
self['location'].replace(window.location)
self['location']['assign'](window.location)
self['location']['replace'](window.location)
self.location = location
self.location = location.href
self.location = window.location
self.location = self.location
self.location = window.location.href
self.location = self.location.href
self.location = location['href']
self.location = window['location']
self.location = window['location'].href
self.location = window['location']['href']
self.location = window.location['href']
self.location = self['location']
self.location = self['location'].href
self.location = self['location']['href']
self.location = self.location['href']
location.assign(self.location)
location.replace(self.location)
window.location.assign(self.location)
window.location.replace(self.location)
self.location.assign(self.location)
self.location.replace(self.location)
location['assign'](self.location)
location['replace'](self.location)
window.location['assign'](self.location)
window.location['replace'](self.location)
window['location'].assign(self.location)
window['location'].replace(self.location)
window['location']['assign'](self.location)
window['location']['replace'](self.location)
self.location['assign'](self.location)
self.location['replace'](self.location)
self['location'].assign(self.location)
self['location'].replace(self.location)
self['location']['assign'](self.location)
self['location']['replace'](self.location)
window.location.href = location
window.location.href = location.href
window.location.href = window.location
window.location.href = self.location
window.location.href = window.location.href
window.location.href = self.location.href
window.location.href = location['href']
window.location.href = window['location']
window.location.href = window['location'].href
window.location.href = window['location']['href']
window.location.href = window.location['href']
window.location.href = self['location']
window.location.href = self['location'].href
window.location.href = self['location']['href']
window.location.href = self.location['href']
location.assign(window.location.href)
location.replace(window.location.href)
window.location.assign(window.location.href)
window.location.replace(window.location.href)
self.location.assign(window.location.href)
self.location.replace(window.location.href)
location['assign'](window.location.href)
location['replace'](window.location.href)
window.location['assign'](window.location.href)
window.location['replace'](window.location.href)
window['location'].assign(window.location.href)
window['location'].replace(window.location.href)
window['location']['assign'](window.location.href)
window['location']['replace'](window.location.href)
self.location['assign'](window.location.href)
self.location['replace'](window.location.href)
self['location'].assign(window.location.href)
self['location'].replace(window.location.href)
self['location']['assign'](window.location.href)
self['location']['replace'](window.location.href)
self.location.href = location
self.location.href = location.href
self.location.href = window.location
self.location.href = self.location
self.location.href = window.location.href
self.location.href = self.location.href
self.location.href = location['href']
self.location.href = window['location']
self.location.href = window['location'].href
self.location.href = window['location']['href']
self.location.href = window.location['href']
self.location.href = self['location']
self.location.href = self['location'].href
self.location.href = self['location']['href']
self.location.href = self.location['href']
location.assign(self.location.href)
location.replace(self.location.href)
window.location.assign(self.location.href)
window.location.replace(self.location.href)
self.location.assign(self.location.href)
self.location.replace(self.location.href)
location['assign'](self.location.href)
location['replace'](self.location.href)
window.location['assign'](self.location.href)
window.location['replace'](self.location.href)
window['location'].assign(self.location.href)
window['location'].replace(self.location.href)
window['location']['assign'](self.location.href)
window['location']['replace'](self.location.href)
self.location['assign'](self.location.href)
self.location['replace'](self.location.href)
self['location'].assign(self.location.href)
self['location'].replace(self.location.href)
self['location']['assign'](self.location.href)
self['location']['replace'](self.location.href)
location['href'] = location
location['href'] = location.href
location['href'] = window.location
location['href'] = self.location
location['href'] = window.location.href
location['href'] = self.location.href
location['href'] = location['href']
location['href'] = window['location']
location['href'] = window['location'].href
location['href'] = window['location']['href']
location['href'] = window.location['href']
location['href'] = self['location']
location['href'] = self['location'].href
location['href'] = self['location']['href']
location['href'] = self.location['href']
location.assign(location['href'])
location.replace(location['href'])
window.location.assign(location['href'])
window.location.replace(location['href'])
self.location.assign(location['href'])
self.location.replace(location['href'])
location['assign'](location['href'])
location['replace'](location['href'])
window.location['assign'](location['href'])
window.location['replace'](location['href'])
window['location'].assign(location['href'])
window['location'].replace(location['href'])
window['location']['assign'](location['href'])
window['location']['replace'](location['href'])
self.location['assign'](location['href'])
self.location['replace'](location['href'])
self['location'].assign(location['href'])
self['location'].replace(location['href'])
self['location']['assign'](location['href'])
self['location']['replace'](location['href'])
window['location'] = location
window['location'] = location.href
window['location'] = window.location
window['location'] = self.location
window['location'] = window.location.href
window['location'] = self.location.href
window['location'] = location['href']
window['location'] = window['location']
window['location'] = window['location'].href
window['location'] = window['location']['href']
window['location'] = window.location['href']
window['location'] = self['location']
window['location'] = self['location'].href
window['location'] = self['location']['href']
window['location'] = self.location['href']
location.assign(window['location'])
location.replace(window['location'])
window.location.assign(window['location'])
window.location.replace(window['location'])
self.location.assign(window['location'])
self.location.replace(window['location'])
location['assign'](window['location'])
location['replace'](window['location'])
window.location['assign'](window['location'])
window.location['replace'](window['location'])
window['location'].assign(window['location'])
window['location'].replace(window['location'])
window['location']['assign'](window['location'])
window['location']['replace'](window['location'])
self.location['assign'](window['location'])
self.location['replace'](window['location'])
self['location'].assign(window['location'])
self['location'].replace(window['location'])
self['location']['assign'](window['location'])
self['location']['replace'](window['location'])
window['location'].href = location
window['location'].href = location.href
window['location'].href = window.location
window['location'].href = self.location
window['location'].href = window.location.href
window['location'].href = self.location.href
window['location'].href = location['href']
window['location'].href = window['location']
window['location'].href = window['location'].href
window['location'].href = window['location']['href']
window['location'].href = window.location['href']
window['location'].href = self['location']
window['location'].href = self['location'].href
window['location'].href = self['location']['href']
window['location'].href = self.location['href']
location.assign(window['location'].href)
location.replace(window['location'].href)
window.location.assign(window['location'].href)
window.location.replace(window['location'].href)
self.location.assign(window['location'].href)
self.location.replace(window['location'].href)
location['assign'](window['location'].href)
location['replace'](window['location'].href)
window.location['assign'](window['location'].href)
window.location['replace'](window['location'].href)
window['location'].assign(window['location'].href)
window['location'].replace(window['location'].href)
window['location']['assign'](window['location'].href)
window['location']['replace'](window['location'].href)
self.location['assign'](window['location'].href)
self.location['replace'](window['location'].href)
self['location'].assign(window['location'].href)
self['location'].replace(window['location'].href)
self['location']['assign'](window['location'].href)
self['location']['replace'](window['location'].href)
window['location']['href'] = location
window['location']['href'] = location.href
window['location']['href'] = window.location
window['location']['href'] = self.location
window['location']['href'] = window.location.href
window['location']['href'] = self.location.href
window['location']['href'] = location['href']
window['location']['href'] = window['location']
window['location']['href'] = window['location'].href
window['location']['href'] = window['location']['href']
window['location']['href'] = window.location['href']
window['location']['href'] = self['location']
window['location']['href'] = self['location'].href
window['location']['href'] = self['location']['href']
window['location']['href'] = self.location['href']
location.assign(window['location']['href'])
location.replace(window['location']['href'])
window.location.assign(window['location']['href'])
window.location.replace(window['location']['href'])
self.location.assign(window['location']['href'])
self.location.replace(window['location']['href'])
location['assign'](window['location']['href'])
location['replace'](window['location']['href'])
window.location['assign'](window['location']['href'])
window.location['replace'](window['location']['href'])
window['location'].assign(window['location']['href'])
window['location'].replace(window['location']['href'])
window['location']['assign'](window['location']['href'])
window['location']['replace'](window['location']['href'])
self.location['assign'](window['location']['href'])
self.location['replace'](window['location']['href'])
self['location'].assign(window['location']['href'])
self['location'].replace(window['location']['href'])
self['location']['assign'](window['location']['href'])
self['location']['replace'](window['location']['href'])
window.location['href'] = location
window.location['href'] = location.href
window.location['href'] = window.location
window.location['href'] = self.location
window.location['href'] = window.location.href
window.location['href'] = self.location.href
window.location['href'] = location['href']
window.location['href'] = window['location']
window.location['href'] = window['location'].href
window.location['href'] = window['location']['href']
window.location['href'] = window.location['href']
window.location['href'] = self['location']
window.location['href'] = self['location'].href
window.location['href'] = self['location']['href']
window.location['href'] = self.location['href']
location.assign(window.location['href'])
location.replace(window.location['href'])
window.location.assign(window.location['href'])
window.location.replace(window.location['href'])
self.location.assign(window.location['href'])
self.location.replace(window.location['href'])
location['assign'](window.location['href'])
location['replace'](window.location['href'])
window.location['assign'](window.location['href'])
window.location['replace'](window.location['href'])
window['location'].assign(window.location['href'])
window['location'].replace(window.location['href'])
window['location']['assign'](window.location['href'])
window['location']['replace'](window.location['href'])
self.location['assign'](window.location['href'])
self.location['replace'](window.location['href'])
self['location'].assign(window.location['href'])
self['location'].replace(window.location['href'])
self['location']['assign'](window.location['href'])
self['location']['replace'](window.location['href'])
self['location'] = location
self['location'] = location.href
self['location'] = window.location
self['location'] = self.location
self['location'] = window.location.href
self['location'] = self.location.href
self['location'] = location['href']
self['location'] = window['location']
self['location'] = window['location'].href
self['location'] = window['location']['href']
self['location'] = window.location['href']
self['location'] = self['location']
self['location'] = self['location'].href
self['location'] = self['location']['href']
self['location'] = self.location['href']
location.assign(self['location'])
location.replace(self['location'])
window.location.assign(self['location'])
window.location.replace(self['location'])
self.location.assign(self['location'])
self.location.replace(self['location'])
location['assign'](self['location'])
location['replace'](self['location'])
window.location['assign'](self['location'])
window.location['replace'](self['location'])
window['location'].assign(self['location'])
window['location'].replace(self['location'])
window['location']['assign'](self['location'])
window['location']['replace'](self['location'])
self.location['assign'](self['location'])
self.location['replace'](self['location'])
self['location'].assign(self['location'])
self['location'].replace(self['location'])
self['location']['assign'](self['location'])
self['location']['replace'](self['location'])
self['location'].href = location
self['location'].href = location.href
self['location'].href = window.location
self['location'].href = self.location
self['location'].href = window.location.href
self['location'].href = self.location.href
self['location'].href = location['href']
self['location'].href = window['location']
self['location'].href = window['location'].href
self['location'].href = window['location']['href']
self['location'].href = window.location['href']
self['location'].href = self['location']
self['location'].href = self['location'].href
self['location'].href = self['location']['href']
self['location'].href = self.location['href']
location.assign(self['location'].href)
location.replace(self['location'].href)
window.location.assign(self['location'].href)
window.location.replace(self['location'].href)
self.location.assign(self['location'].href)
self.location.replace(self['location'].href)
location['assign'](self['location'].href)
location['replace'](self['location'].href)
window.location['assign'](self['location'].href)
window.location['replace'](self['location'].href)
window['location'].assign(self['location'].href)
window['location'].replace(self['location'].href)
window['location']['assign'](self['location'].href)
window['location']['replace'](self['location'].href)
self.location['assign'](self['location'].href)
self.location['replace'](self['location'].href)
self['location'].assign(self['location'].href)
self['location'].replace(self['location'].href)
self['location']['assign'](self['location'].href)
self['location']['replace'](self['location'].href)
self['location']['href'] = location
self['location']['href'] = location.href
self['location']['href'] = window.location
self['location']['href'] = self.location
self['location']['href'] = window.location.href
self['location']['href'] = self.location.href
self['location']['href'] = location['href']
self['location']['href'] = window['location']
self['location']['href'] = window['location'].href
self['location']['href'] = window['location']['href']
self['location']['href'] = window.location['href']
self['location']['href'] = self['location']
self['location']['href'] = self['location'].href
self['location']['href'] = self['location']['href']
self['location']['href'] = self.location['href']
location.assign(self['location']['href'])
location.replace(self['location']['href'])
window.location.assign(self['location']['href'])
window.location.replace(self['location']['href'])
self.location.assign(self['location']['href'])
self.location.replace(self['location']['href'])
location['assign'](self['location']['href'])
location['replace'](self['location']['href'])
window.location['assign'](self['location']['href'])
window.location['replace'](self['location']['href'])
window['location'].assign(self['location']['href'])
window['location'].replace(self['location']['href'])
window['location']['assign'](self['location']['href'])
window['location']['replace'](self['location']['href'])
self.location['assign'](self['location']['href'])
self.location['replace'](self['location']['href'])
self['location'].assign(self['location']['href'])
self['location'].replace(self['location']['href'])
self['location']['assign'](self['location']['href'])
self['location']['replace'](self['location']['href'])
self.location['href'] = location
self.location['href'] = location.href
self.location['href'] = window.location
self.location['href'] = self.location
self.location['href'] = window.location.href
self.location['href'] = self.location.href
self.location['href'] = location['href']
self.location['href'] = window['location']
self.location['href'] = window['location'].href
self.location['href'] = window['location']['href']
self.location['href'] = window.location['href']
self.location['href'] = self['location']
self.location['href'] = self['location'].href
self.location['href'] = self['location']['href']
self.location['href'] = self.location['href']
location.assign(self.location['href'])
location.replace(self.location['href'])
window.location.assign(self.location['href'])
window.location.replace(self.location['href'])
self.location.assign(self.location['href'])
self.location.replace(self.location['href'])
location['assign'](self.location['href'])
location['replace'](self.location['href'])
window.location['assign'](self.location['href'])
window.location['replace'](self.location['href'])
window['location'].assign(self.location['href'])
window['location'].replace(self.location['href'])
window['location']['assign'](self.location['href'])
window['location']['replace'](self.location['href'])
self.location['assign'](self.location['href'])
self.location['replace'](self.location['href'])
self['location'].assign(self.location['href'])
self['location'].replace(self.location['href'])
self['location']['assign'](self.location['href'])
self['location']['replace'](self.location['href'])
location.reload()
location['reload']()
window.location.reload()
window['location'].reload()
window.location['reload']()
window['location']['reload']()
self.location.reload()
self['location'].reload()
self.location['reload']()
self['location']['reload']()
Чтобы перезагрузить страницу с помощью JavaScript, используйте:
window.location.reload();
Если вы положили
window.location.reload(true);
в начале вашей страницы без каких-либо других условий, определяющих, почему этот код работает, страница загружается, а затем продолжает перезагружаться до закрытия браузера.
location.href = location.href;
Используя кнопку или просто поместите ее в тег "a" (якорь):
<input type="button" value="RELOAD" onclick="location.reload();" />
Попробуйте это для других нужд:
Location Objects has three methods --
assign() Used to load a new document
reload() Used to reloads the current document.
replace() Used to replace the current document with a new one
Чтобы сделать его простым и простым, используйте location.reload()
.
Вы также можете использовать location.reload(true)
, если хотите получить что-то с сервера.
Спасибо, этот пост был очень полезен не только для перезагрузки страницы с предложенным ответом, но также и для того, чтобы дать мне идею разместить значок кнопки jQuery UI на кнопке:
<button style="display:block; vertical-align:middle; height:2.82em;"
title="Cargar nuevamente el código fuente sin darle un [Enter] a la dirección en la barra de direcciones"
class="ui-state-active ui-corner-all ui-priority-primary"
onclick="javascript:window.location.reload(true);">
<span style="display:inline-block;" class="ui-icon ui-icon-refresh"></span>
[<b>CARGAR NUEVAMENTE</b>]
</button>
Автоматическая перезагрузка страницы через 20 секунд.
<script>
window.onload = function() {
setTimeout(function () {
location.reload()
}, 20000);
};
</script>
Это должно работать:
window.location.href = window.location.href.split( '#' )[0];
или
var x = window.location.href;
x = x.split( '#' );
window.location.href = x[0];
Я предпочитаю это по следующим причинам:
В качестве альтернативы вы можете использовать последний официальный метод для этой задачи
window.location.reload()
Используйте эту кнопку, чтобы обновить страницу
<input type="button" value="Reload Page" onClick="document.location.reload(true)">
Вы можете просто использовать
window.location=document.URL
где document.URL получает текущий URL страницы, а window.location перезагружает его.
Я знаю, что все ответы выше будут делать трюк в большинстве случаев, но очень простой и простой кросс-браузер обходной путь будет заключаться в том, чтобы избежать сложности трюков, специфичных для браузера, только создав тег привязки <a>
.
Для пользователей jQuery:
var hiddenReloadLink = $('<a>'),
hiddenReloadLinkElem = $(a);
hiddenReloadLinkElem.attr('href', link).css('display', 'none !important');
$('body').append(a);
hiddenReloadLinkElem.get(0).click();
return false;
Альтернатива для чистого JS находится по всему Интернету.