?
本文介紹下,用js刷新當前頁面的幾種方法,包括reload方法、replace方法、自動刷新方法等。有需要的朋友參考下吧
如何實現(xiàn)刷新當前頁面呢?借助js你將無所不能。
1,reload 方法,該方法強迫瀏覽器刷新當前頁面。
語法:location.reload([bForceGet])??
參數(shù): bForceGet, 可選參數(shù), 默認為 false,從客戶端緩存里取當前頁。true, 則以 GET 方式,從服務端取最新的頁面, 相當于客戶端點擊 F5("刷新")
2,replace 方法,該方法通過指定URL替換當前緩存在歷史里(客戶端)的項目,因此當使用replace方法之后,你不能通過“前進”和“后退”來訪問已經(jīng)被替換的URL。
語法: location.replace(URL)??
通常使用: location.reload() 或者是 history.go(0) 來做。
此方法類似客戶端點F5刷新頁面,所以頁面method="post"時,會出現(xiàn)"網(wǎng)頁過期"的提示。
因為Session的安全保護機制。
當調(diào)用 location.reload() 方法時, aspx頁面此時在服務端內(nèi)存里已經(jīng)存在, 因此必定是 IsPostback 的。
如果有這種應用: 需要重新加載該頁面,也就是說期望頁面能夠在服務端重新被創(chuàng)建,期望是 Not IsPostback 的。
這里,location.replace() 就可以完成此任務。被replace的頁面每次都在服務端重新生成。
代碼: location.replace(location.href);
返回并刷新頁面:
location.replace(document.referrer);
document.referrer //前一個頁面的URL
不要用 history.go(-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,頁面自動刷新:把如下代碼加入<head>區(qū)域中
代碼如下:
<meta http-equiv="refresh" content="20">
其中20指每隔20秒刷新一次頁面.
2,頁面自動跳轉:把如下代碼加入<head>區(qū)域中
代碼如下:
<meta http-equiv="refresh" content="20;url=https://www.weidianyuedu.com">
其中20指隔20秒后跳轉到https://www.weidianyuedu.com頁面
3,頁面自動刷新js版
代碼如下:
<script language="JavaScript">
function myrefresh()
{
?? window.location.reload();
}
setTimeout('myrefresh()',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>中調(diào)用以下語句即可。
代碼如下:
<body οnlοad="opener.location.reload()"> 開窗時刷新
<body onUnload="opener.location.reload()"> 關閉時刷新
<script language="javascript">
window.opener.document.location.reload()
</script>文章來源:http://www.zghlxwxcb.cn/news/detail-737374.html
來源:微點閱讀? ?https://www.weidianyuedu.com文章來源地址http://www.zghlxwxcb.cn/news/detail-737374.html
到了這里,關于js刷新頁面方法合集分享的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!