简体   繁体   English

Javascript通过添加URL来获取URL并重定向

[英]Javascript get url and redirect by adding to it

I have a situation where I need to get the current page url and redirect to a new page by adding to the current page url. 我遇到一种情况,我需要获取当前页面的URL并通过添加到当前页面的URL来重定向到新页面。 For example: 例如:

http://www.mywebsite.com/page1/page2 http://www.mywebsite.com/page1/page2

needs to redirect to: 需要重定向到:

http://www.mywebsite.com/page1/page2/page3 http://www.mywebsite.com/page1/page2/page3

I need it to be relative because "/page1/page2/" will always be different, but "page3" will always be the same. 我需要它是相对的,因为“ / page1 / page2 /”将始终不同,但是“ page3”将始终相同。

I've tried: location.href = "./page3"; 我试过了:location.href =“ ./page3”; but that does not work. 但这不起作用。 The result is: http://www.mywebsite.com/page1/page3 结果是: http : //www.mywebsite.com/page1/page3

Any thoughts? 有什么想法吗?

也许这样吗?:

location.href = location.pathname + "/page3";

Get and Set URL using either window.location.href or document.URL; 使用window.location.hrefdocument.URL;获取和设置URL document.URL;

window.location.href = window.location.href + "/page3";

Should do what you're looking for. 应该做您想要的。

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

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