Home  >  Article  >  Web Front-end  >  How to refresh the page with JavaScript

How to refresh the page with JavaScript

青灯夜游
青灯夜游Original
2021-07-19 19:04:004681browse

Page refresh method: 1. "history.go(0)" statement; 2. "location.reload()" statement; 3. "location=location" statement; 4. "location.assign(location )" statement and so on.

How to refresh the page with JavaScript

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)

  • ##document.execCommand('Refresh')

  • window.navigate(location)

  • location.replace(location)

  • document.URL=location.href

reload method

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 method

This 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">

The page automatically refreshes the js version

<script language="JavaScript">function myrefresh(){
   window.location.reload();
}
setTimeout(&#39;myrefresh()&#39;,1000); //指定1秒刷新一次</script>

[Recommended learning:

javascript advanced tutorial

]

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn