首頁  >  文章  >  web前端  >  Javascript刷新頁面方法及location.reload()用法介紹

Javascript刷新頁面方法及location.reload()用法介紹

小云云
小云云原創
2018-02-03 11:31:038600瀏覽

本文主要和大家介紹Javascript刷新頁面方法及location.reload()用法介紹,希望透過本文能幫助到大家,需要的朋友可以參考下,希望能幫助到大家。

Javascript刷新頁面的幾個方法:

1    history.go(0)
2    location.reload()
3    location =location
4    location.assign(location)
5    document.execCommand('Refresh')
6    window.navigate(location)
7  URL=location.href

自動刷新頁面的方法:

#1.頁面自動刷新:將如下程式碼加入93f0f5c25f18dab9d176bd4f6de5d30e區域中



<meta http-equiv="refresh" content="20">

其中20指每隔20秒刷新一次頁面.

2.頁面自動跳轉:把如下程式碼加入93f0f5c25f18dab9d176bd4f6de5d30e區域中



<meta http-equiv="refresh" content="20;url=http://www.wyxg.com">

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

3.頁面自動刷新js版



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

在javascript程式設計中,多使用location.reload實作頁面重新整理。

範例:

window.location.href=window.location.href;
window.location.reload;

經測試,這兩句在某些情況下可以取代location.reload(true);

而不會出現重試對話框達到刷新的效果

在js中實作刷新頁面的方法有很多種,在js中有一個location.reload()函數,它就可以實作我們想要的功能。 

window.location.reload(true) //瀏覽器重新從伺服器請求資源,在http請求頭中不會包含快取標記。

範例1,刷新目前頁面

   
<script>
window.location.reload();
</script>

   

#例2,JS實作刷新iframe的方法 

用iframe的name屬性定位

   
<input type="button" name="Button" value="Button" onclick="document.frames(&#39;ifrmname&#39;).location.reload()">
或
<input type="button" name="Button" value="Button" onclick="document.all.ifrmname.document.location.reload()">

範例3,首先,定義一個iframe

<iframe method="post" id ="IFrameName" src="aa.htm" ></iframe>

   

aa.htm頁面的內容:

<input type ="button" value ="刷新" onclick ="aa()"/>
function aa() { (php中文网 www.php.cn 编辑整理)
//parent.location.replace(parent.location.href);//服务器端重新创建页面
parent.document.location.reload();//相当于F5
//window.location.href(parent.location.href);//iframe内容重定向
}

注意:

window.location. reload;
刷新時如果提交資料的動作,則會出現對話框!

解決方法:

   
window.location.href=window.location.href;
window.location.reload;

   

刷新父視窗:

window.opener.location.href=window.opener.location.href;
window.opener.location.reload();

相關推薦:

Javascript中刷新頁面的範例詳解

利用JavaScript實作不重新整理頁面預覽上傳圖片方法教學

JS重新整理頁面總和!多種JS刷新頁面代碼!

以上是Javascript刷新頁面方法及location.reload()用法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn