常用的javascript方法之刷新頁面的幾種方法(part6)

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.wyxg.com">

其中20指隔20秒後跳轉到http://www.wyxg.com頁面

3.頁面自動刷新js版

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

ASP.NET如何輸出刷新父窗口腳本語句

this.response.write("<script>opener.location.reload();</script>"); 
this.response.write("<script>opener.window.location.href = opener.window.location.href;</script>"); 
Response.Write("<script language=javascript>opener.window.navigate(''你要刷新的頁.asp'');</script>")

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>

首先頁面跳轉、刷新、重定向要看實施這個動作的物件,一般有三個物件:本頁面的刷新跳轉、父頁面的刷新跳轉、最外層頁面的刷新跳轉
一般window.location.href是操作本頁面的位址,parent.location.href是操作父頁面的位址,top.location.href是操作最外層頁面的位址。
無論是本頁面刷新跳轉、父頁面刷新跳轉還是最外層頁面刷新跳轉都有這麼幾種方式:
1 history.go(0)
2 location.reload()
3 location=location
4 location.assign(location)
5 document.execCommand('Refresh')
6 window.navigate(location)針對IE,firefox不支援
7 location.replace(location)

history.go(0):刷新本頁
history.go(-1):回到上一頁
history.go(-2):回到上一頁的上一頁

reload()跟history.go(0)相同

window.location與assign相同

在實際應用的時候,重新刷新頁面通常使用: location.reload() 或者是 history.go(0) 來做。因為這種做法就像是用戶端點F5刷新頁面,所以頁面的method="post"的時候,會出現「網頁過期」的提示。那是因為Session的安全保護機制。可以想到: .net中當調用 location.reload() 方法的時候, aspx頁面此時在服務端記憶體裡已經存在, 因此必定是isPostback 的。
而這時我們希望頁面重新載入一遍而不是重新提交的時候,我們可以用location.replace() 就可以完成此任務。被replace的頁面每次都在服務端重新生成。你可以這麼寫: window.location.replace(location.href)

replace用法跟 location.href 相同,但用 replace 導到下頁後,就不能再回到上一頁.document.location.href和document.location.replace都可以實現從A頁面切換到B頁面,但他們的區別是:用document.location.href切換後,可以退回到原頁面。而用document.location.replace切換後,不可以通過「後退」退回到原頁面。

Javascript代碼:

history.go(0);
//history的另外兩個方法
history.back(); //後退到前一頁
history.forward();//前進到當前頁之後打開的頁
window.location.reload();
window.location = window.location.href;
window.location.assign(window.location.href);//刷新當前頁
window.location.assign("HTTP://www.google.com");//跳轉到HTTP://www.google.com
window.location.replace(window.location.href);//刷新當前頁
window.location.replace("HTTP://www.google.com");//跳轉到HTTP://www.google.com

//父頁面跳轉
parent.window.history.go(0);
parent.window.location.reload();
parent.window.location = window.location.href;
parent.window.location.assign(window.location.href);//刷新父頁面
parent.window.location.assign("HTTP://www.google.com");//父頁面跳轉到HTTP://www.google.com
parent.window.location.replace(window.location.href);//刷新父頁面
parent.window.location.replace("HTTP://www.google.com");//父頁面跳轉到HTTP://www.google.com
///最外層則用top關鍵字

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值