1. 程式人生 > >JS(history,location的使用)返回上一頁以及返回上一頁並重新整理

JS(history,location的使用)返回上一頁以及返回上一頁並重新整理

window.history.go(-1);//返回上一頁不重新整理
window.history.back();  //返回上一頁  
window.location.href = document.referrer;//返回上一頁並重新整理,真正實現頁面後退並重新整理頁面
history.go(1);//前進一頁
history.forward(); //前進一頁
history.length;//用length屬性檢視歷史中的頁面數

因為windows物件引用不是必須的。所以windows.history.go() == history.go()的。

<button onclick="history.go(-1);">返回上一頁</button>
<a href="javascript:history.back();">返回上一頁</a>


history.go(-1)和history.back()的區別
1.history.go(-1)表示後退與重新整理。如資料有改變也隨之改變
2.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 ;