瀏覽器窗口有一個history對象,用來保存瀏覽歷史。
檢查瀏覽器是否支持 if (window.history){
// 支持History API
} else {
// 不支持
}
history對象提供了一系列方法,允許在瀏覽歷史之間移動。
其中包括
history.back(); 回退
history.forward(); 前進
history.go(0)//刷新當前頁面;
HTML5為history對象添加了兩個新方法,history.pushState()和history.replaceState(),用來在瀏覽歷史中添加和修改記錄。
都會改變瀏覽器標簽欄中的URL值,區別在於pushState()會將之前的地址記錄在history對象中,通過back()可以返回前一頁,replaceState()則不能返回
我用到的是history.replaceState() 可以實現修改url 且不刷新頁面
let shortURL =“你的新URL”
window.history.replaceState(null, null, shortURL)
參數實現修改
window.history.replaceState(null, null, "?name=zhangsan")