js refresh page method collection sharing

 

This article introduces several methods to refresh the current page using js, including reload method, replace method, automatic refresh method, etc. Friends in need can refer to it.

How to refresh the current page? With js you can do anything.

1. Reload method, this method forces the browser to refresh the current page.
Syntax: location.reload([bForceGet])  
Parameters: bForceGet, optional parameter, default is false, retrieve the current page from the client cache. If true, use GET method to get the latest page from the server, which is equivalent to the client clicking F5 ("Refresh")

2. Replace method, this method replaces the item currently cached in the history (client) by specifying the URL. Therefore, after using the replace method, you cannot access the replaced URL through "forward" and "back".
Syntax: location.replace(URL)  
is usually done using: location.reload() or history.go(0).
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 Session’s security protection mechanism.
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 accomplish this task. The replaced page is regenerated on the server every time.
Code: location.replace(location.href);

Return and refresh the page:

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.
Attached:

Several ways to refresh the page using Javascript:

code show as below:


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. The page automatically refreshes: add the following code to the <head> area

The code is as follows:
<meta http-equiv="refresh" content="20">

Among them, 20 refers to refreshing the page every 20 seconds.


2. The page automatically jumps: add the following code to the <head> area

code show as below:


<meta http-equiv="refresh" content="20;url=https://www.weidianyuedu.com">

Among them, 20 fingers will jump to the page https://www.weidianyuedu.com after 20 seconds.


3. The page automatically refreshes the js version

code show as below:


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



4. JS refresh frame script statement

code show as below:


//Refresh the page containing the frame using  
<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> )
//Refresh the page of another frame using  
<script language=JavaScript>
   parent.another FrameID.location .reload();
</script>

If you want to refresh when the window is closed or you want to refresh when the window is opened, just call the following statement in <body>.

code show as below:


<body οnlοad="opener.location.reload()"> Refresh when the window is open
<body onUnload="opener.location.reload()"> Refresh when it is closed
<script language="javascript">
window.opener.document.location .reload()
</script>

Source: Weidian Reading    https://www.weidianyuedu.com

Guess you like

Origin blog.csdn.net/weixin_45707610/article/details/131752769
Recommended