目前,我正在使用此代码让用户通过其 uuid
访问私有(private)页面。
if (!!$.cookie('my_uuid_cookie')) {
var my_uuid = $.cookie("my_uuid_cookie")
var target = window.location.href + '?uuid=' + my_uuid;
window.location.href = target;
}
首先,我检查 url 是否不包含任何 uuid
。如果没有,我会触发上面的代码。
话虽这么说,您可能会注意到,window.location.href
强制页面重新加载,这看起来不太好。有没有办法“默默地”将查询字符串添加到我的网址中?
(页面加载时不需要 URL,用户必须通过单击它来切换另一个功能。这就是需要查询字符串的时刻。)
最佳答案
试试这个:
window.history.pushState("object or string", "Title", "/new-url");
请注意,仅适用于 HTML5 浏览器
https://developer.mozilla.org/en-US/docs/Web/API/History_API#The_pushState()_method
关于javascript - 如何将查询字符串添加到页面网址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39275961/