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

js method to refresh page

不言
Release: 2018-03-31 16:55:54
Original
1987 people have browsed it

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. true, then use GET method to get the latest page from the server, which is equivalent to the client clicking F5 ("Refresh")

2, replace method, which replaces the current cache in the history by specifying the URL (client-side) project, 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 point F5 to refresh the page, so when the page method="post", a "webpage expired" prompt will appear.
Because of the security protection mechanism of Session.
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 //The 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 using Javascript:


Copy code The code is as follows :

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

How to automatically refresh the page:
1, the page automatically refreshes: add the following code to the area


Copy code The code is as follows:

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

The 20 refers to refreshing the page every 20 seconds.
2, the page automatically jumps: add the following code In the area


##Copy code The code is as follows:

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

The 20 refers to jumping after 20 seconds Go to http://www.jb51.net page


3, the page will automatically refresh the js version


Copy code The code is as follows:

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

4, JS refresh frame script statement


Copy code The code is as follows :

//刷新包含该框架的页面用   
<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 the window is closed or you want to refresh when the window is opened, just call the following statement in .


Copy code The code is as follows:

<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 explain how to do it.
frame.html consists of two pages: top (top.html) and bottom (bottom.html). The code is as follows:

##Copy code

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 refresh bottom.html (i.e., the page below). You can use the following seven statements. Which one is better for you to decide? Done. The code of the

top.html page is as follows:


Copy code

The code 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 pops up after the page is loaded.



Copy code

The code is as follows:

<!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:



Copy code

code show as below:


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('Refresh')
6 window.navigate(location)
7 location.replace(location)
8 document.URL=location.href


二、自动刷新页面
1.页面自动刷新:把如下代码加入区域中
<meta http-equiv="refresh" content="20">
其中20指每隔20秒刷新一次页面.
2.页面自动跳转:把如下代码加入区域中
<meta http-equiv="refresh" content="20;url=http://www.jb51.net">
其中20指隔20秒后跳转到http://www.jb51.net页面
3.页面自动刷新js版


[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]


三、java在写Servler,Action等程序时,要操作返回页面的话(如谈出了窗口,操作完成以后,关闭当前页面,刷新父页面)

复制代码 代码如下:

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

四、JS刷新框架的脚本语句
1.如何刷新包含该框架的页面用

复制代码 代码如下:

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


2.子窗口刷新父窗口

复制代码 代码如下:

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

3.如何刷新另一个框架的页面用 (上面的实例以说明了)

复制代码 代码如下:

语句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.如果想关闭窗口时刷新或者想开窗时刷新的话,在中调用以下语句即可。

<body onload="opener.location.reload()"> 
开窗时刷新 
<body onUnload="opener.location.reload()"> 
关闭时刷新
Copy after login

复制代码 代码如下:

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

相关推荐:

js刷新页面方法大全

The above is the detailed content of js method to refresh page. For more information, please follow other related articles on 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