close
轉自 https://www.zoearthmoon.net/blog/program/item/1910.html
--
大家好
偶而會遇到一種情況,就是需要修改上一頁的網址,或是目前網址且不轉換頁面
在 gmail 是很常見到的功能,那應該怎麼做呢?
寫得蠻詳細的
舉例:
var stateObj = { value: "testing...1" }; history.pushState(stateObj , 'page add1', 'newpage1.html'); var stateObj = { value: "testing...2" }; history.pushState(stateObj , 'page add2', 'newpage2.html'); window.onpopstate = function(event) { alert("目前位置:"+document.location+" ..."+history.state.value); };
按上一頁會出現 testing...1
再按下一頁會出現 testing...2
也就是說他會直接修改目前網址且新增紀錄
並且修改 history.state 裡面傳送的 stateObj 這物件
再舉例
history.replaceState({page: 9}, 'title 9', 'newpage9.html');
輸入後會發現紀錄並沒有新增,而是直接替代了目前網址紀錄
基本上常用的只有 replaceState, pushState。
至於 第2個參數title 確實沒有什麼作用,我也試不太出來
要特別注意的是,必須在 IE 10 之後才有辦法呼叫 history 物件中的 replaceState, pushState。
給大家參考囉
感恩!
--
全站熱搜