如何實(shí)現(xiàn)刷新當(dāng)前頁(yè)面呢?借助js你將無(wú)所不能。
1,reload 方法,該方法強(qiáng)迫瀏覽器刷新當(dāng)前頁(yè)面。
語(yǔ)法:location.reload([bForceGet])
參數(shù): bForceGet, 可選參數(shù), 默認(rèn)為 false,從客戶端緩存里取當(dāng)前頁(yè)。true, 則以 GET 方式,從服務(wù)端取最新的頁(yè)面, 相當(dāng)于客戶端點(diǎn)擊 F5("刷新")
2,replace 方法,該方法通過(guò)指定URL替換當(dāng)前緩存在歷史里(客戶端)的項(xiàng)目,因此當(dāng)使用replace方法之后,你不能通過(guò)“前進(jìn)”和“后退”來(lái)訪問(wèn)已經(jīng)被替換的URL。
語(yǔ)法: location.replace(URL)
通常使用: location.reload() 或者是 history.go(0) 來(lái)做。
此方法類似客戶端點(diǎn)F5刷新頁(yè)面,所以頁(yè)面method="post"時(shí),會(huì)出現(xiàn)"網(wǎng)頁(yè)過(guò)期"的提示。
因?yàn)镾ession的安全保護(hù)機(jī)制。
當(dāng)調(diào)用 location.reload() 方法時(shí), aspx頁(yè)面此時(shí)在服務(wù)端內(nèi)存里已經(jīng)存在, 因此必定是 IsPostback 的。
如果有這種應(yīng)用: 需要重新加載該頁(yè)面,也就是說(shuō)期望頁(yè)面能夠在服務(wù)端重新被創(chuàng)建,期望是 Not IsPostback 的。
這里,location.replace() 就可以完成此任務(wù)。被replace的頁(yè)面每次都在服務(wù)端重新生成。
代碼: location.replace(location.href);
返回并刷新頁(yè)面:
location.replace(document.referrer);
document.referrer //前一個(gè)頁(yè)面的URL
不要用 history.go(-1),或 history.back();來(lái)返回并刷新頁(yè)面,這兩種方法不會(huì)刷新頁(yè)面。
附:
Javascript刷新頁(yè)面的幾種方法:
復(fù)制代碼代碼如下:
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
自動(dòng)刷新頁(yè)面的方法:
1,頁(yè)面自動(dòng)刷新:把如下代碼加入<head>區(qū)域中
復(fù)制代碼代碼如下:
<meta http-equiv="refresh" content="20">
其中20指每隔20秒刷新一次頁(yè)面.
2,頁(yè)面自動(dòng)跳轉(zhuǎn):把如下代碼加入<head>區(qū)域中
復(fù)制代碼代碼如下:
<meta http-equiv="refresh" content="20;url=https://www.jb51.net">
其中20指隔20秒后跳轉(zhuǎn)到https://www.jb51.net頁(yè)面
3,頁(yè)面自動(dòng)刷新js版
復(fù)制代碼代碼如下:
<script language="JavaScript">
function myrefresh()
{
_window.location.reload();
}
setTimeout('myrefresh()',1000); //指定1秒刷新一次
</script>
4,JS刷新框架的腳本語(yǔ)句
復(fù)制代碼代碼如下:
//刷新包含該框架的頁(yè)面用
<script language=JavaScript>
parent.location.reload();
</script>
//子窗口刷新父窗口
<script language=JavaScript>
self.opener.location.reload();
</script>
( 或 <a href="javascript:opener.location.reload()">刷新</a> )
//刷新另一個(gè)框架的頁(yè)面用
<script language=JavaScript>
parent.另一FrameID.location.reload();
</script>
如果想關(guān)閉窗口時(shí)刷新或想開窗時(shí)刷新,在<body>中調(diào)用以下語(yǔ)句即可。
復(fù)制代碼代碼如下:
<body onload="opener.location.reload()"> 開窗時(shí)刷新
<body onUnload="opener.location.reload()"> 關(guān)閉時(shí)刷新
<script language="javascript">
window.opener._document.location.reload()
</script>
引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]
復(fù)制代碼代碼如下:
1 PrintWriter out = response.getWriter();
2 out.write("<script type=\"text/javascript\">");
3 ////子窗口刷新父窗口
4 out.write("self.opener.location.reload();");
5 //關(guān)閉窗口
6 out.write("window.opener=null;");
7 out.write("window.close();");
8 out.write("</script>");
復(fù)制代碼代碼如下:
<script language=JavaScript>
parent.location.reload();
</script>
復(fù)制代碼代碼如下:
<script language=JavaScript>
self.opener.location.reload();
</script>
復(fù)制代碼代碼如下:
語(yǔ)句1. window.parent.frames[1].location.reload();
語(yǔ)句2. window.parent.frames.bottom.location.reload();
語(yǔ)句3. window.parent.frames["bottom"].location.reload();
語(yǔ)句4. window.parent.frames.item(1).location.reload();
語(yǔ)句5. window.parent.frames.item('bottom').location.reload();
語(yǔ)句6. window.parent.bottom.location.reload();
語(yǔ)句7. window.parent['bottom'].location.reload();
復(fù)制代碼代碼如下:
<script language="javascript">
window.opener._document.location.reload()
</script>