js set to automatically refresh

How to refresh the current page it? Js will help you do anything.

1, reload method that 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")

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: 

Here to three pages named 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 
Copy the code code is as follows:

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

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

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

3. How to refresh the page using another framework (above examples to illustrate) 
Copy the code code is as follows:

语句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 code is as follows:

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

 

To every simple thing, it is not simple; to every trivial thing to do, is extraordinary! Believe in yourself, miracles ~~

Guess you like

Origin www.cnblogs.com/supereast/p/10935544.html