如何在 javascript 中更改当前 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8866118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the current URL in javascript?
提问by Maan Sahir
On my website:
在我的网站上:
http://mywebsite.com/1.html
I want to use the
我想使用
window.location.pathname
to get the last part of the url:
获取网址的最后一部分:
1.html
and since I have all my webpages in numbers I want to add 1 to the current url so that when I click a button it will redirect me to the next page:
并且由于我的所有网页都是数字,因此我想在当前 url 上加 1,这样当我单击按钮时,它会将我重定向到下一页:
var url = 'http://mywebsite.com/' + window.location.pathname;
function nextImage (){
url = url + 1;
}
any ideas why this is not working ?
任何想法为什么这不起作用?
采纳答案by jfriend00
Your example wasn't working because you are trying to add 1 to a string that looks like this: "1.html". That will just get you this "1.html1" which is not what you want. You have to isolate the numeric part of the string and then convert it to an actual number before you can do math on it. After getting it to an actual number, you can then increase its value and then combine it back with the rest of the string.
您的示例不起作用,因为您试图将 1 添加到如下所示的字符串中:“1.html”。那只会让你得到这个“1.html1”,这不是你想要的。您必须隔离字符串的数字部分,然后将其转换为实际数字,然后才能对其进行数学运算。在得到一个实际数字后,你可以增加它的值,然后将它与字符串的其余部分组合起来。
You can use a custom replace function like this to isolate the various pieces of the original URL and replace the number with an incremented number:
您可以使用这样的自定义替换函数来隔离原始 URL 的各个部分,并用递增的数字替换数字:
function nextImage() {
return(window.location.href.replace(/(\d+)(\.html)$/, function(str, p1, p2) {
return((Number(p1) + 1) + p2);
}));
}
You can then call it like this:
然后你可以这样称呼它:
window.location.href = nextImage();
Demo here: http://jsfiddle.net/jfriend00/3VPEq/
演示在这里:http: //jsfiddle.net/jfriend00/3VPEq/
This will work for any URL that ends in some series of digits followed by .html and if you needed a slightly different URL form, you could just tweak the regular expression.
这适用于任何以一系列数字结尾的 URL,后跟 .html,如果你需要一个稍微不同的 URL 形式,你可以调整正则表达式。
回答by Steven Penny
This is more robust:
这更健壮:
mi = location.href.split(/(\d+)/);
no = mi.length - 2;
os = mi[no];
mi[no]++;
if ((mi[no] + '').length < os.length) mi[no] = os.match(/0+/) + mi[no];
location.href = mi.join('');
When the URL has multiple numbers, it will change the last one:
当 URL 有多个数字时,它会更改最后一个:
http://mywebsite.com/8815/1.html
It supports numbers with leading zeros:
它支持带前导零的数字:
http://mywebsite.com/0001.html
回答by albanx
Even it is not a good way of doing what you want try this hint: var url = MUST BE A NUMER FIRST
即使它不是做你想做的事情的好方法试试这个提示:var url = MUST BE A NUMER FIRST
function nextImage (){
url = url + 1;
location.href='http://mywebsite.com/' + url+'.html';
}
回答by j08691
What you're doing is appending a "1" (the string) to your URL. If you want page 1.html link to page 2.html you need to take the 1 out of the string, add one to it, then reassemble the string.
您正在做的是将“1”(字符串)附加到您的 URL。如果您希望第 1.html 页链接到第 2.html 页,您需要从字符串中取出 1,向其添加一个,然后重新组合该字符串。
Why not do something like this:
为什么不做这样的事情:
var url = 'http://mywebsite.com/1.html';
var pageNum = parseInt( url.split("/").pop(),10 );
var nextPage = 'http://mywebsite.com/'+(pageNum+1)+'.html';
nextPage will contain the url http://mywebsite.com/2.htmlin this case. Should be easy to put in a function if needed.
在这种情况下,nextPage 将包含 url http://mywebsite.com/2.html。如果需要,应该很容易放入一个函数中。