javascript - 使用 pushstate 添加查询字符串

标签 javascript jquery query-string browser-history pushstate

我正在做类似下面的事情

if(history.pushState)
{
    var stateObject = { dummy: true };
    var url = window.location.protocol
        + "//"
        + window.location.host
        + window.location.pathname
        + '?myNewUrlQuery=1';
    history.pushState(stateObject, jQuery(document).find('title').text(), + url);
}

我的路径名是正确的,但查询字符串是 Nan。例如我得到的结果是

http://example.com/mypage/Nan

代替

http://example.com/page/?myNewUrlQuery=1

请告诉我是否可以使用 pushstate 方法在 URL 中添加查询字符串。如果是,任何工作示例都将非常有帮助。

提前致谢。

最佳答案

+ url

url 变量前有一个一元加号运算符,可将其转换为数字。因为它不是包含数字的字符串,所以您得到的是 Not A Number。删除 +

关于javascript - 使用 pushstate 添加查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24465030/

相关文章:

javascript - 如何在 jQuery 中查找多个 div 值中的 div?

javascript - 单击按钮时隐藏 div

url - mod_rewrite - 将查询字符串转换为路径

javascript - 表单的 get 方法与 Angular $http.get 请求

javascript - CreateJS - 缩放 Canvas 不会缩放鼠标坐标

javascript - 如何以非 Ajax 方式使用 jQuery(或 Javascript)从 API 调用中获取 JSON 结果?

jquery - Google PageSpeed Insights - 需要修复

javascript - flashvars 不适用于从 url 读取的字母数字值

javascript位置搜索url中的多个查询参数

javascript - jQuery.trigger() 刷新时不触发