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

How to refresh the page with JavaScript

青灯夜游
Release: 2023-01-07 11:44:16
Original
4783 people have browsed it

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])
Copy after login

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)
Copy after login

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);
Copy after login

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">
Copy after login

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">
Copy after login

The page automatically refreshes the js version

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

[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!

Related labels:
Statement of this Website
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template