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 JSX in Vue

What is JSX JSX is a syntax extension of Javascri...

Implementation of MySQL asc and desc data sorting

Data sorting asc, desc 1. Single field sorting or...

Detailed explanation of browser negotiation cache process based on nginx

This article mainly introduces the detailed proce...

HTML Form Tag Tutorial (4):

Suppose now you want to add an item like this to ...

Introduction to Docker containers

Docker Overview Docker is an open source software...

Simple usage example of vue recursive component

Preface I believe many students are already famil...

Simple implementation method of Linux process monitoring and automatic restart

Purpose: Under Linux, the server program may be d...

How to use dynamic parameters and calculated properties in Vue

1. Dynamic parameters Starting from 2.6.0, you ca...

Neon light effects implemented with pure CSS3

This is the effect to be achieved: You can see th...

Solution to ES memory overflow when starting docker

Add the jvm.options file to the elasticsearch con...

How to change the root password in a container using Docker

1. Use the following command to set the ssh passw...

Implementation of vscode custom vue template

Use the vscode editor to create a vue template, s...

Detailed explanation of PHP+nginx service 500 502 error troubleshooting ideas

Overview When a 500 or 502 error occurs during ac...