Home > Web Front-end > JS Tutorial > body text

A complete list of js refresh page methods

高洛峰
Release: 2016-12-17 15:20:55
Original
1543 people have browsed 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. true, then use the GET method to fetch the latest page from the server, which is equivalent to the client clicking F5 ("Refresh")

2, replace method, which replaces the item currently cached in the history (client) by specifying the URL , so after using the replace method, you cannot access the replaced URL through "forward" and "back".
Syntax: location.replace(URL)
Usually use: location.reload() or history.go(0) to do it.
This method is similar to the client pressing 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 complete 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.
Attachment:

Several methods of Javascript refreshing the 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
Copy after login

Methods to automatically refresh the page:
1. The page automatically refreshes: add the following code to the area

<meta http-equiv="refresh" content="20">
Copy after login

The 20 fingers refresh every 20 seconds Once the page.
2, the page automatically jumps: add the following code to the area

<meta http-equiv="refresh" content="20;url=http://www.php.cn">
Copy after login

20 fingers will jump to the http://www.php.cn page after 20 seconds
3, the page automatically refreshes js version

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

4, JS refresh frame script statement

//刷新包含该框架的页面用   
<script language=JavaScript>
   parent.location.reload();
</script>
//子窗口刷新父窗口
<script language=JavaScript>
    self.opener.location.reload();
</script>
( 或 <a href="javascript:opener.location.reload()">刷新</a>   )
//刷新另一个框架的页面用   
<script language=JavaScript>
   parent.另一FrameID.location.reload();
</script>
Copy after login

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

<body onload="opener.location.reload()"> 开窗时刷新
<body onUnload="opener.location.reload()"> 关闭时刷新
<script language="javascript">
window.opener.document.location.reload()
</script>
Copy after login

First, let’s look at a simple example:

The following takes three pages named frame.html, top.html, and bottom.html as an example to specifically explain how to do it.
frame.html consists of two pages: top (top.html) and bottom (bottom.html). The code is as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE> frame </TITLE> 
</HEAD> 
<frameset rows="50%,50%"> 
<frame name=top src="top.html"> 
<frame name=bottom src="bottom.html"> 
</frameset> 
</HTML>
Copy after login

Now assume that top.html (i.e., the page above) has seven buttons to implement bottom.html ( That is, to refresh the page below), you can use the following seven statements. It’s up to you which one is easier to use. The code of the
top.html page is as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE> top.html </TITLE> 
</HEAD> 
<BODY> 
<input type=button value="刷新1" onclick="window.parent.frames[1].location.reload()"><br> 
<input type=button value="刷新2" onclick="window.parent.frames.bottom.location.reload()"><br> 
<input type=button value="刷新3" onclick="window.parent.frames[&#39;bottom&#39;].location.reload()"><br> 
<input type=button value="刷新4" onclick="window.parent.frames.item(1).location.reload()"><br> 
<input type=button value="刷新5" onclick="window.parent.frames.item(&#39;bottom&#39;).location.reload()"><br> 
<input type=button value="刷新6" onclick="window.parent.bottom.location.reload()"><br> 
<input type=button value="刷新7" onclick="window.parent[&#39;bottom&#39;].location.reload()"><br> 
</BODY> 
</HTML>
Copy after login

The following is the source code of the bottom.html page. In order to prove that the page below has indeed been refreshed, a dialog box will pop up after the page is loaded.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE> bottom.html </TITLE> 
</HEAD> 
<BODY onload="alert(&#39;我被加载了!&#39;)"> 
<h1>This is the content in bottom.html.</h1> 
</BODY> 
</HTML>
Copy after login

Explain:

1.window指代的是当前页面,例如对于此例它指的是top.html页面。 
2.parent指的是当前页面的父页面,也就是包含它的框架页面。例如对于此例它指的是framedemo.html。 
3.frames是window对象,是一个数组。代表着该框架内所有子页面。 
4.item是方法。返回数组里面的元素。 
5.如果子页面也是个框架页面,里面还是其它的子页面,那么上面的有些方法可能不行。 
附: 
Javascript刷新页面的几种方法: 
1 history.go(0) 
2 location.reload() 
3 location=location 
4 location.assign(location) 
5 document.execCommand(&#39;Refresh&#39;) 
6 window.navigate(location) 
7 location.replace(location) 
8 document.URL=location.href
Copy after login

2. Automatically refresh the page
1. Automatically refresh the page: Add the following code to the area
<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 area

20 of them will jump to the http://www.jb51.net page after 20 seconds
3. The page automatically refreshes the js version

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

3. Java is writing Servler, Action, etc. When executing a program, if you want to return to the page (such as opening the window, after the operation is completed, close the current page and refresh the parent page)

1 PrintWriter out = response.getWriter(); 
2 out.write("<script type=\"text/javascript\">"); 
3 ////子窗口刷新父窗口 
4 out.write("self.opener.location.reload();"); 
5 //关闭窗口 
6 out.write("window.opener=null;"); 
7 out.write("window.close();"); 
8 out.write("</script>");
Copy after login

4. JS script statement to refresh the frame
1. How to refresh the page containing the frame with

<script language=JavaScript> 
parent.location.reload(); 
</script>
Copy after login

2. The child window refreshes the parent window

<script language=JavaScript> 
self.opener.location.reload(); 
</script>
Copy after login

3. How to refresh the page of another frame (the above example illustrates)

语句1. window.parent.frames[1].location.reload(); 
语句2. window.parent.frames.bottom.location.reload(); 
语句3. window.parent.frames["bottom"].location.reload(); 
语句4. window.parent.frames.item(1).location.reload(); 
语句5. window.parent.frames.item(&#39;bottom&#39;).location.reload(); 
语句6. window.parent.bottom.location.reload(); 
语句7. window.parent[&#39;bottom&#39;].location.reload();
Copy after login

4. If you want to refresh when the window is closed or you want to refresh when the window is opened, in Just call the following statement in .

Refresh when opening the window

Refresh when closing

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



For more js refresh page related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!