首頁  >  文章  >  web前端  >  js刷新頁面方法大全

js刷新頁面方法大全

高洛峰
高洛峰原創
2016-12-17 15:20:551529瀏覽

如何實現刷新當前頁面呢?借助js你將無所不能。

1,reload 方法,該方法強迫瀏覽器刷新當前頁面。
語法:location.reload([bForceGet])   
參數: bForceGet, 可選參數, 預設為 false,從客戶端快取取當一頁。 true, 則以GET 方式,從服務端取最新的頁面, 相當於客戶端點擊F5("刷新")

2,replace 方法,該方法通過指定URL替換當前緩存在歷史裡(客戶端)的項目,因此當使用replace方法之後,你不能透過「前進」和「後退」來存取已經被替換的URL。
文法: location.replace(URL)   
通常使用: location.reload() 或是 history.go(0) 來做。
此方法類似客戶端點F5刷新頁面,所以頁面method="post"時,會出現"網頁過期"的提示。
因為Session的安全保護機制。
當呼叫 location.reload() 方法時, aspx頁面此時在服務端記憶體裡已經存在, 因此必定是 IsPostback 的。
如果有這種應用: 需要重新載入該頁面,也就是說期望頁面能夠在服務端重新被創建,期望是 Not IsPostback 的。
這裡,location.replace() 就可以完成此任務。被replace的頁面每次都會在服務端重新產生。
程式碼: location.replace(location.href);

回傳並重新整理頁面:

location.replace(document.referrer);
document.referrer //前一個頁面的URL

1),或history.back();來返回並刷新頁面,這兩種方法不會刷新頁面。
附:

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每隔120秒刷新一次頁.
2,頁面自動跳轉:把如下程式碼加入

區域中
<meta http-equiv="refresh" content="20;url=http://www.php.cn">

其中20指隔20秒後跳到http://www.php.cn頁
3,頁自動刷新js版

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

4,JS刷新框架的腳本語句

//刷新包含该框架的页面用   
<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>

如果想關閉視窗時刷新或想開窗時刷新,在

中調用以下語句即可。
<body onload="opener.location.reload()"> 开窗时刷新
<body onUnload="opener.location.reload()"> 关闭时刷新
<script language="javascript">
window.opener.document.location.reload()
</script>

一、先來看一個簡單的例子: 

下面以三個頁面分別命名為frame.html、top.html、bottom.html為例來具體說明如何做。
frame.html 由上(top.html)下(bottom.html)兩個頁面組成,程式碼如下: 

<!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 (即上面的頁面) 有七個button來實作對bottom.html (即下面的頁) 的刷新,可以用以下七種語句,哪個好用自己看著辦了。 
top.html 頁面的程式碼如下: 

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

下方是bottom.html頁面原始碼,為了證明下方頁面的確被刷新了,在裝載完頁面彈出一個對話框。

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

解釋一下: 

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

二、自動刷新頁 
1.頁面自動刷新:把如下代碼加入

區域中 
<meta http-equiv="refresh" content="20"> 
其中20指每隔20秒刷新一次頁面. 
2.頁面自動跳轉:如下碼加入區域中 
 
其中20指隔20秒後跳到http://www.jb51.net頁 
3.頁面自動刷新js版 
<script language="JavaScript"> 
function myrefresh() 
{ 
window.location.reload(); 
} 
setTimeout(&#39;myrefresh()&#39;,1000); //指定1秒刷新一次 
</script>

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

四、JS刷新框架的腳本語句 
1.如何刷新包含該框架的頁用 

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

2.子窗口刷新父視窗 

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

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

4.如果想關閉視窗時刷新或想開窗時刷新的話,在

中調用以下語句即可。
 
開窗時刷新 
 
關閉時刷新 
<script language="javascript"> 
window.opener.document.location.reload() 
</script>



🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜🎜 js刷新頁面方法大全相關文章請關注PHP中文網! 🎜
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn