Home > Article > Web Front-end > How to refresh the page with JavaScript
Page refresh method: 1. "history.go(0)" statement; 2. "location.reload()" statement; 3. "location=location" statement; 4. "location.assign(location )" statement and so on.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Several ways to refresh the page using Javascript:
history.go(0)
location.reload()
##location=location
location.assign(location)
This method forces the browser to refresh the current page.
Syntax:
location.reload([bForceGet])
Parameters: bForceGet, optional parameter, default is false, get the current page from the client cache. true, then use GET method to get the latest page from the server, which is equivalent to the client clicking F5 ("Refresh")
replace methodThis method is specified by The URL replaces the item currently cached in the history (client-side), so after using the replace method, you cannot access the replaced URL via "forward" and "back".
Syntax:
location.replace(URL)
Usually use: location.reload() or history.go(0) to do it.
This method is similar to the client point F5 to refresh the page, so when the page method="post", a "webpage expired" prompt will appear.
Because of the security protection mechanism of Session.
When the location.reload() method is called, the aspx page already exists in the server memory, so it must be IsPostback.
If there is such an application: The page needs to be reloaded, which means that the page is expected to be re-created on the server side, and the expectation is Not IsPostback.
Here, location.replace() can complete this task. The replaced page is regenerated on the server every time.
Code:
location.replace(location.href);How to automatically refresh the page:
Automatically refresh the page: Add the following code to the area, where 20 refers to Refresh the page every 20 seconds.
<meta http-equiv="refresh" content="20">
The page automatically jumps: add the following code to the area, where 20 refers to jumping to the https://www.baidu.com page after 20 seconds
<meta http-equiv="refresh" content="20;url="https://www.baidu.com">
<script language="JavaScript">function myrefresh(){ window.location.reload(); } setTimeout('myrefresh()',1000); //指定1秒刷新一次</script>
The above is the detailed content of How to refresh the page with JavaScript. For more information, please follow other related articles on the PHP Chinese website!