首页 > 解决方案 > 在不刷新页面的情况下更改 URL 参数

问题描述

我一直在尝试在不重新加载站点的情况下更改 URL 的参数,但是,我不能 100% 确定我的代码有什么问题。URL 中发生的事情是删除所有参数并将其替换为一个简单的字符串。即 http://localhost:50754/Signup555

我想从http://localhost:50754/Signup?A=1&B=2&C=3更改为http://localhost:50754/Signup?A=1&B=2&C=6

  function changeQueryString(searchString, documentTitle) {
        documentTitle = typeof documentTitle !== 'Pid' ? documentTitle : document.title;
        var urlSplit = (window.location.href).split("?");
        var obj = {
            Title: documentTitle,
            Url: urlSplit[0] + searchString
        };
        history.pushState(obj, obj.Title, obj.Url);
    }

<a onclick="changeQueryString('555', 'Pid')"> change it</a>

提前致谢。

标签: javascriptjquery

解决方案


你的功能应该是:

  function changeQueryString(searchString, documentTitle) {
     window.history.replaceState(null, null, window.location.pathname+"?A="+searchString[0]+"&B="+searchString[1]+"&C="+searchString[2]);
    }

<a onclick="changeQueryString('555', 'Pid')"> change it</a>

推荐阅读