如何使用JavaScript重新加载页面

JavaScript

乐Eva

2020-03-10

如何使用JavaScript重新加载页面?

我需要一种适用于所有浏览器的方法。

第502篇《如何使用JavaScript重新加载页面》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

12个回答
GilJinJin 2020.03.10

Use history (more here)

history.go()
2020.03.10

You can simply use

window.location=document.URL

where document.URL gets the current page URL and window.location reloads it.

GilTony 2020.03.10

Use this button to refresh the page

DEMO

<input type="button" value="Reload Page" onClick="document.location.reload(true)">
神无Tom 2020.03.10

Using a button or just put it inside an "a" (anchor) tag:

<input type="button" value="RELOAD" onclick="location.reload();" />

Try these for other needs:

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
猴子逆天 2020.03.10

Automatic reload page after 20 seconds.

<script>
    window.onload = function() {
        setTimeout(function () {
            location.reload()
        }, 20000);
     };
</script>
村村阿飞 2020.03.10
location.href = location.href;
路易卡卡西 2020.03.10

To make it easy and simple, use location.reload(). You can also use location.reload(true) if you want to grab something from the server.

GO蛋蛋 2020.03.10

If you put

window.location.reload(true);

at the beginning of your page with no other condition qualifying why that code runs, the page will load and then continue to reload itself until you close your browser.

神奇 2020.03.10

This works for me:

function refresh() {    
    setTimeout(function () {
        location.reload()
    }, 100);
}

http://jsfiddle.net/umerqureshi/znruyzop/

Jim理查德泡芙 2020.03.10

To reload a page using JavaScript, use:

window.location.reload();
米亚猴子 2020.03.10

You can perform this task using window.location.reload();. As there are many ways to do this but I think it is the appropriate way to reload the same document with JavaScript. Here is the explanation

JavaScript window.location object can be used

  • to get current page address (URL)
  • to redirect the browser to another page
  • to reload the same page

window: in JavaScript represents an open window in a browser.

location: in JavaScript holds information about current URL.

The location object is like a fragment of the window object and is called up through the window.location property.

location object has three methods:

  1. assign(): used to load a new document
  2. reload(): used to reload current document
  3. replace(): used to replace current document with a new one

So here we need to use reload(), because it can help us in reloading the same document.

So use it like window.location.reload();.

Online demo on jsfiddle

To ask your browser to retrieve the page directly from the server not from the cache, you can pass a true parameter to location.reload(). This method is compatible with all major browsers, including IE, Chrome, Firefox, Safari, Opera.

路易Stafan 2020.03.10

I was looking for some information regarding reloads on pages retrieved with POST requests, such as after submitting a method="post" form.

To reload the page keeping the POST data, use:

window.location.reload();

To reload the page discarding the POST data (perform a GET request), use:

window.location.href = window.location.href;

Hopefully this can help others looking for the same information.

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android