天天看點

JavaScript多種頁面重新整理方法小結1,reload 方法2,replace 方法

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

不要用 history.go(-1),或 history.back();來傳回并重新整理頁面,這兩種方法不會重新整理頁面。

附:Javascript重新整理頁面的幾種方法:

history.go(0)

location.reload()

location=location

location.assign(location)

document.execCommand('Refresh')

window.navigate(location)

location.replace(location)

document.URL=location.href

自動重新整理頁面的方法:

1,頁面自動重新整理:把如下代碼加入<head>區域中

<meta http-equiv="refresh" content="20">       (其中20指每隔20秒重新整理一次頁面.)

2,頁面自動跳轉:把如下代碼加入<head>區域中

<meta http-equiv="refresh" content="20;url=http://www.baidu.com">    (其中20指隔20秒後跳轉到http://www.baidu.comt頁面)

3,頁面自動重新整理js版

<script type="text/javascript">

function myrefresh()

{

   window.location.reload();

}

setTimeout('myrefresh()',1000); //指定1秒重新整理一次

</script>

4,JS重新整理架構的腳本語句

//重新整理包含該架構的頁面用   

<script type="text/javascript">

   parent.location.reload();

</script>

//子視窗重新整理父視窗

<script type="text/javascript">

    self.opener.location.reload();

</script>

( 或 <a href="javascript:opener.location.reload()" target="_blank" rel="external nofollow" >重新整理</a>   )

//重新整理另一個架構的頁面用   

<script type="text/javascript">

   parent.另一FrameID.location.reload();

</script>

如果想關閉視窗時重新整理或想開窗時重新整理,在<body>中調用以下語句即可。

<body οnlοad="opener.location.reload()"> 開窗時重新整理

<body onUnload="opener.location.reload()"> 關閉時重新整理

<script>

    window.opener.document.location.reload()

</script>

繼續閱讀