如何将查询字符串添加到页面网址?

时间:2016-09-01 15:49:43

标签: javascript jquery cookies

目前我正在使用此代码为用户提供uuid访问私人网页的权限。

if (!!$.cookie('my_uuid_cookie')) {
      var my_uuid = $.cookie("my_uuid_cookie")
      var target = window.location.href + '?uuid=' + my_uuid;
      window.location.href = target;
}

首先,我检查网址是否包含uuid。如果没有,我解雇上面的代码。

据说你可能会注意到,window.location.href迫使页面重新加载,这看起来并不好。有没有办法“默默地”将查询字符串添加到我的网址?

(页面加载时不需要URL,用户必须通过单击来切换另一个函数。这就是需要查询字符串的时刻。)

1 个答案:

答案 0 :(得分:1)

试试这个:

window.history.pushState("object or string", "Title", "/new-url");

请注意,这仅适用于HTML5浏览器

https://developer.mozilla.org/en-US/docs/Web/API/History_API#The_pushState()_method