A link refresh page and js refresh page usage examples

A link refresh page and js refresh page usage examples
1. How to use the link:

Copy code
The code is as follows:

<a href="javascript:history.go(-1);">Return to the previous page</a>
<a href="#top">Refresh current page</a>

The #top effect didn't work for me, but it worked when I directly wrote <a href="">Refresh current page</a>.

2. How to refresh the current page with JS

The reload method forces the browser to refresh the current page.
Syntax: location.reload([bForceGet])

Parameter: bForceGet, optional parameter, default is false, get the current page from the client cache. true, then the latest page is retrieved from the server using GET, which is equivalent to the client clicking F5 ("refresh")

The replace method replaces the item currently cached in the history (client) by the specified URL. Therefore, after using the replace method, you cannot access the replaced URL by "forward" and "backward".
Syntax: location.replace(URL)

In actual application, when refreshing the page, we usually use: location.reload() or history.go(0) to do it. Because this approach is like the client clicking F5 to refresh the page, when the page method="post", a "page expired" prompt will appear. That's because of the Session security protection mechanism. It can be imagined that: when the location.reload() method is called, the aspx page already exists in the server's memory, so it must be IsPostback. If there is such an application: We need to reload the page, that is, we expect the page to be recreated on the server, and we expect it to be Not IsPostback. Here, location.replace() can accomplish this task. The replaced page is regenerated on the server each time.

You can write: location.replace(location.href);

Go back and refresh the page:

Copy code
The code is as follows:

location.replace(document.referrer);
document.referrer //URL of the previous page

Do not use history.go(-1) or history.back(); to return and refresh the page. These two methods will not refresh the page.

Attachment:
Several ways to refresh the page with Javascript:

1 history.go(0)
2 location.reload()
3 location=location
4 location.assign(location)
5 document.execCommand('Refresh')
6 window.navigate(location)
7 location.replace(location)
8 document.URL=location.href

How to automatically refresh the page:
1. Automatic page refresh: Add the following code to the <head> area
<meta http-equiv="refresh" content="20">
20 means refreshing the page every 20 seconds.

2. Automatic page jump: Add the following code to the <head> area
<meta http-equiv="refresh" content="20;url=http://www.wyxg.com">
20 means jumping to http://www.wyxg.com after 20 seconds.

3. Page automatic refresh js version

Copy code
The code is as follows:

<script language="JavaScript">
function myrefresh()
{
window.location.reload();
}
setTimeout('myrefresh()',1000); //Specify refresh once every 1 second
</script>

JS refresh frame script statement

Copy code
The code is as follows:

//How to refresh the page containing the frame
<script language=JavaScript>
parent.location.reload();
</script>
//The child window refreshes the parent window
<script language=JavaScript>
self.opener.location.reload();
</script>
(or <a href="javascript:opener.location.reload()">refresh</a> )
//How to refresh the page of another frame
<script language=JavaScript>
parent.anotherFrameID.location.reload();
</script>

If you want to refresh when closing the window or when opening the window, just call the following statement in <body>.

Copy code
The code is as follows:

<body onload="opener.location.reload()"> Refresh when opening the window
<body onUnload="opener.location.reload()"> Refresh on close
<script language="javascript">
window.opener.document.location.reload()
</script>

<<:  Problems and solutions for deploying Nginx+KeepAlived cluster dual-active architecture in VMware

>>:  30 excellent examples of color matching in web design

Recommend

Implementing a shopping cart with native JavaScript

This article shares the specific code of JavaScri...

Introduction to the use of base link tag base

<br />When you click the link, the web page ...

How to install JDK8 on Windows

1. Download: http://www.oracle.com/technetwork/ja...

How to generate PDF and download it in Vue front-end

Table of contents 1. Installation and introductio...

Analysis and solution of MySQL connection throwing Authentication Failed error

[Problem description] On the application side, th...

Detailed explanation of Js class construction and inheritance cases

The definition and inheritance of classes in JS a...

Detailed explanation of nginx configuration file interpretation

The nginx configuration file is mainly divided in...

Analysis of the Principle and Method of Implementing Linux Disk Partition

remember: IDE disk: the first disk is hda, the se...

25 Examples of News-Style Website Design

bmi Voyager Pitchfork Ulster Grocer Chow True/Sla...

How to use vite to build vue3 application

1. Installation Tip: There is currently no offici...

Detailed explanation of the binlog log analysis tool for monitoring MySQL: Canal

Canal is an open source project under Alibaba, de...

CentOS7.x uninstall and install MySQL5.7 operation process and encoding format modification method

1. Uninstalling MySQL 5.7 1.1查看yum是否安裝過mysql cd y...

MySQL 5.6.23 Installation and Configuration Environment Variables Tutorial

This article shares the installation and configur...