简体   繁体   English

无需重新加载即可替换 url 中的页码

[英]replace page number in url without reloading

I'm trying to work out how to replace a page number in my URL when ajax page loading.我正在尝试解决如何在 ajax 页面加载时替换我的 URL 中的页码。 Here's what I have right now:这是我现在拥有的:

var current_url = window.location.href;
var replaced_url = current_url.toString();
new_url = replaced_url.replace("/page=\d+/", "page="+page);
history.pushState('page'+page, document.title, new_url);

Problem is, the page number never gets replaced.问题是,页码永远不会被替换。

Testing it on a URL like:在 URL 上对其进行测试,例如:

http://127.0.0.1/2020/09/some-title-here/page=2#comments

It just always says page=2, it's never replace - why?它总是说 page=2,它永远不会替换 - 为什么?

You're trying to replace a static string with another string, rather than using RegEx, don't stringify the RegEx format: replaced_url.replace(/page=\d+/, "page=" + page)您正在尝试用另一个字符串替换 static 字符串,而不是使用正则表达式,不要对正则表达式格式进行字符串化: replaced_url.replace(/page=\d+/, "page=" + page)

 var current_url = "http://127.0.0.1/2020/09/some-title-here/page=2#comments"; var replaced_url = current_url.toString(); var new_url = replaced_url.replace(/page=\d+/, "page=69"); console.log(new_url);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM