【小知识点】js无需刷新在url地址添加参数

今天后端同事找我,问我一个关于js无需刷新在url地址添加参数的方法。

然后我百度啊,终于在一篇文章找到了办法,非常简单的一句代码。

 window.history.pushState({}, 0,  window.location.href + '?' + 参数);

假如你的url地址是demo.html,然后在url部分需要添加参数“type=weixin”,然后你可以这样写:

 window.history.pushState({}, 0,  window.location.href + '?type=weixin');

然后你就会不刷新就可以在url后面添加参数,这个url已经变成“demo.html?type=weixin”。

问题解决啦。

感谢作者:http://www.qttc.net/201303292.html

原文地址:https://www.cnblogs.com/winteronlyme/p/7743752.html