Five ways for JS to refresh the current page

The reload method, which forces the browser to refresh the current page.

Syntax: location.reload ([bForceGet]) 

Parameters: bForceGet, optional parameters, default is false, fetch the current page from the client cache. true, then get the latest page from the server by GET, which is equivalent to the client clicking F5 ("refresh")

replace method, this method replaces the current cached item in the history (client) by specifying the URL, so after using the replace method, you cannot access the URL that has been replaced by "forward" and "backward".
Syntax: location.replace (URL)   

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

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

Go back 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.

Attachment:
Several methods of Javascript refresh page:
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">
where 20 refers to refreshing the page every 20 seconds.

2. The page automatically jumps: Add the following code to the <head> area
<meta http-equiv = "refresh" content = "20; url = // www.jb51.net">
where 20 refers to jump after 20 seconds Go to //www.jb51.net page

3. The page automatically refreshes the js version

Copy the code code as follows:

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


Script statement of JS refresh framework

 

// How to refresh the page containing the frame with   
<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 with   
<script language = JavaScript>
   parent. Another FrameID.location.reload ();
</ script>

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

<body οnlοad = "opener.location.reload ()"> Refresh when opening window
<body onUnload = "opener.location.reload ()"> Refresh when closing

<script language="javascript">
window.opener.document.location.reload()
</script>

Guess you like

Origin www.cnblogs.com/jiangyunfeng/p/12747295.html