1. 程式人生 > >window.location.replace和window.location.href的區別

window.location.replace和window.location.href的區別

sdn style -h 默認 .net oca height ack tps

簡單說說:有3個jsp頁面(1.jsp, 2.jsp, 3.jsp)。

進系統默認的是1.jsp ,當我進入2.jsp的時候,

2.jsp裏面用window.location.replace("3.jsp");與用window.location.href("3.jsp");從用戶界面來看是沒有什麽區別的,

但是當3.jsp頁面有一個“返回”按鈕,調用window.history.go(-1);wondow.history.back();方法的時候,

一點這個返回按鈕區別就出來了,

當用window.location.replace("3.jsp");連到3.jsp頁面的話,3.jsp頁面中的調用window.history.go(-1);wondow.history.back();方法是不好用的,會返回到1.jsp。

當用window.location.href("3.jsp");連到3.jsp頁面的話,3.jsp頁面中的調用window.history.go(-1);wondow.history.back();方法是好用的,會返回2.jsp。

因為window.location.replace("3.jsp");是不向服務器發送請求的跳轉,

而window.history.go(-1);wondow.history.back();方法是根據服務器記錄的請求決定該跳到哪個頁面的,

所以會跳到系統默認頁面1.jsp 。window.location.href("3.jsp");是向服務器發送請求的跳轉,window.history.go(-1);wondow.history.back();方法是根據服務器記錄的請求決定該跳到哪個頁面的,所以就可以返回到2.jsp。


原文:https://blog.csdn.net/weixin_40944162/article/details/81257405

window.location.replace和window.location.href的區別