修改浏览器url地址或者参数 , 实现不刷新页面

浏览器窗口有一个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")

 

posted @ 2019-11-12 16:04  老北京(LBJ)  阅读(2907)  评论(0编辑  收藏  举报