Home >Web Front-end >JS Tutorial >js method to refresh page
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
How to automatically refresh the page:
1, the page automatically refreshes: add the following code to the 93f0f5c25f18dab9d176bd4f6de5d30e area
Copy code The code is as follows:
<meta http-equiv="refresh" content="20">
The 20 refers to refreshing the page every 20 seconds.
2, the page automatically jumps: add the following code 93f0f5c25f18dab9d176bd4f6de5d30eIn the area
##Copy code The code is as follows:
<meta http-equiv="refresh" content="20;url=http://www.jb51.net">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('myrefresh()',1000); //指定1秒刷新一次 </script>
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>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 6c04bd5ca3fcae76e30b72ad730ca86d.
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>
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:
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>
top.html page is as follows:
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['bottom'].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('bottom').location.reload()"><br>
<input type=button value="刷新6" onclick="window.parent.bottom.location.reload()"><br>
<input type=button value="刷新7" onclick="window.parent['bottom'].location.reload()"><br>
</BODY>
</HTML>
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('我被加载了!')">
<h1>This is the content in bottom.html.</h1>
</BODY>
</HTML>
code show as below: 复制代码 代码如下: 四、JS刷新框架的脚本语句 复制代码 代码如下: 复制代码 代码如下: 3.如何刷新另一个框架的页面用 (上面的实例以说明了) 复制代码 代码如下: 4.如果想关闭窗口时刷新或者想开窗时刷新的话,在6c04bd5ca3fcae76e30b72ad730ca86d中调用以下语句即可。 复制代码 代码如下: 相关推荐:
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.页面自动刷新:把如下代码加入93f0f5c25f18dab9d176bd4f6de5d30e区域中
e1e9bde4ad2e5e5180f8390be8b7a81a
其中20指每隔20秒刷新一次页面.
2.页面自动跳转:把如下代码加入93f0f5c25f18dab9d176bd4f6de5d30e区域中
6b19efeed4a48774ca99eef67b4f5877
其中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>");
1.如何刷新包含该框架的页面用 <script language=JavaScript>
parent.location.reload();
</script>
2.子窗口刷新父窗口 <script language=JavaScript>
self.opener.location.reload();
</script>
语句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('bottom').location.reload();
语句6. window.parent.bottom.location.reload();
语句7. window.parent['bottom'].location.reload();
<body onload="opener.location.reload()">
开窗时刷新
<body onUnload="opener.location.reload()">
关闭时刷新
<script language="javascript">
window.opener.document.location.reload()
</script>
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!