js achieve page refresh

1。 reload

reload method, which forces the browser to refresh the current page.
Syntax: location.reload ([bForceGet])
Parameters: bForceGet, optional parameters, defaults to false, taken from the client cache the current page. true, places GET way, take the latest page from the server, the client is equivalent to clicking F5 ( "Refresh")

reload () method is used to reload the current document.
If the method does not require parameters or parameter is false, it will use the HTTP If-Modified-Since header to detect whether the document has changed on the server. If the document has changed, reload () will download the document again. If the document is not changed, the method to load the document from the cache. This effect and the user clicks the browser's Refresh button is exactly the same.

2, replace method replaces the current cache in history where (client) project by specifying a URL, and therefore when using replace method, you can not "forward" and "back" to access has been replaced in the URL.
Syntax: location.replace (URL)
commonly used: location.reload () or history.go (0) to do.
This method is similar to the customer endpoint F5 to refresh the page, so the page when method = "post", there will be "page expired" message.
Because of security protection mechanisms Session.
When you call location.reload () method, aspx page at this time already exists in the server memory, and therefore must be IsPostback of.
If this application: the need to reload the page, that page can expect to be re-created on the server, the expectation is Not IsPostback.
Here, location.replace () to complete this task. Is replace pages are regenerated every time the server.
Code: location.replace (location.href);

Go back and refresh the page:

location.replace (document.referrer);
former document.referrer // URL for a page

Do not use history.go (-1), or history.back (); to go back and refresh the page, these two methods will not refresh the page.
Attachment:

Javascript refresh the page several ways:


Copy the code 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

 

A method of automatically refresh the page:
1, page automatically refreshes: The following code to <head> region


Copy the code code is as follows:

<meta http-equiv="refresh" content="20">

 

Wherein 20 refers to the page refresh every 20 seconds.
2, page automatically jump: the following code to <head> region


Copy the code code is as follows:

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

 

Wherein the spacer means 20 to 20 sec skip http://www.jb51.net page
3, page automatic refresh js Edition


Copy the code code is as follows:

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

 

4, JS script statements frame refresh


Copy the code code is as follows:

// refresh frame containing the page with the
<= the JavaScript Language Script>
parent.location.reload ();
</ Script>
// child window to refresh the parent window
<= the JavaScript Language Script>
self.opener.location.reload ();
</ Script>
(or refresh <a href="javascript:opener.location.reload()"> </a>)
// frame refresh another page with
<= the JavaScript Language Script>
parent. another FrameID.location .reload ();
</ Script>

 

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


Copy the code code is as follows:

<body onload = "opener.location.reload () "> refresh window
<body onUnload = "opener.location.reload () "> Refresh closed
<Script Language = "JavaScript">
window.opener.document.location .reload ()
</ Script>

 

A first look at a simple example:

The following were named to three pages frame.html, top.html, bottom.html example to specify how to do.
frame.html by the upper (top.html) (bottom.html) two pages, as follows:
copy the code 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>


Now suppose top.html (ie, the page above) has seven button to achieve refresh bottom.html (ie following page), you can use the following seven statements, which own choice of easy to use.
top.html page code is as follows:
copy the code 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>


Here is bottom.html page source code, in order to prove that indeed the bottom of the page is refreshed, and the page has finished loading in a dialog box.
Copy the code 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>


Explain:
Copy the code code is as follows:

1.window refers to the current page, for example, it refers to this embodiment top.html page.
2.parent refers to the current page's parent page, that is, it contains the framework page. For example, this embodiment it refers framedemo.html.
3.frames is a window object is an array. Representing all the sub-pages within that framework.
4.item method. Returns an array of elements inside.
5. If the sub-page framework is also a page, there is still the other sub-pages, so some of the above methods may not work.
Annex:
Javascript page refresh several ways:
. 1 history.go (0)
2 location.reload ()
. 3 = LOCATION LOCATION
. 4 location.assign (LOCATION)
. 5 document.execCommand ( 'the Refresh')
. 6 window.navigate (LOCATION)
the location.replace. 7 (LOCATION)
. 8 = document.URL the location.href


Second, the auto refresh page
1. The page automatically refreshes: The following code to <head> region
<meta http-equiv = "refresh " content = "20">
wherein 20 refers to the page refresh every 20 seconds.
2. Automatic page Jump: the following code to <head> region
<meta http-equiv = "refresh " content = "20; url = http: //www.jb51.net">
wherein 20 refers to the interval 20-sec skip to http : //www.jb51.net page
3. page automatically refresh js version

[Ctrl + A Select All Note: For introducing external Js need to refresh to perform]


Three, java when writing Servler, Action and other programs, then return to the page you want to operate (such as talking out of the window, after the operation is completed, close the current page, refresh the parent page)
Copy the code code is as follows:

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>");


Fourth, the refresh script statements JS framework
1. How to refresh the page containing the frame with
the following copy the code Code:

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


2. The child window parent window to refresh
copy the code code is as follows:

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


3. How to refresh the page with another frame (in the above described example)
copy the code following code:

语句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();


4. If you want to refresh or want to refresh the window when the window is closed, then you can call the following statements in the <body>.
<body onload = "opener.location.reload () ">
refresh window
<body onUnload = "opener.location.reload () ">
Refresh closed
copy the code following code:

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

Guess you like

Origin www.cnblogs.com/ldsice/p/11106347.html