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

How to use VUE and Canvas to implement a Thunder Fighter typing game

Today we are going to implement a Thunder Fighter...

Web interview Vue custom components and calling methods

Import: Due to project requirements, we will enca...

How to draw the timeline with vue+canvas

This article example shares the specific code of ...

Detailed explanation of the spacing problem between img tags

IMG tag basic analysis In HTML5, the img tag has ...

Docker data volume container creation and usage analysis

A data volume container is a container specifical...

Table related arrangement and Javascript operation table, tr, td

Table property settings that work well: Copy code ...

How to change the Ali source in Ubuntu 20.04

Note that this article does not simply teach you ...

How to analyze SQL execution plan in MySQL through EXPLAIN

Preface In MySQL, we can use the EXPLAIN command ...

VUE introduces the implementation of using G2 charts

Table of contents About G2 Chart use Complete cod...

mysql 8.0.19 win10 quick installation tutorial

This tutorial shares the installation tutorial of...

A brief introduction to React

Table of contents 1. CDN introduction 1.1 react (...

TCP third handshake data transmission process diagram

The process packets with the SYN flag in the RFC7...

How to view and clean up Docker container logs (tested and effective)

1. Problem The docker container logs caused the h...

HTML table markup tutorial (22): row border color attribute BORDERCOLORLIGHT

Within rows, light border colors can be defined i...