javascript - 在不刷新页面的情况下更改 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>
提前致谢。
解决方案
你的功能应该是:
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>
推荐阅读
- linux - 更新 glibc 后:分段错误(核心转储)
- android - 我的应用程序无法发送获取位置的请求
- spring-boot - 如何在另一个具有循环依赖的单声道中使用单声道
- python - 在 Pandas DataFrame 中转置选定的 MultiIndex 级别
- external - 如何将外部库添加到 Spree 扩展?
- javascript - 从范围内的数组中获取唯一值?
- excel - Removeduplicates 无法用于大小取决于数据范围的数组
- php - 使用 Carbon::now() 插入日期时间,但添加一个空属性 Laravel
- linux - Drupal 8:如何为节点使用私有文件系统
- spring-boot - Spring Boot:Kotlin 和 MongoRepository:执行全文搜索