简体   繁体   English

在不同浏览器中使用javascript的相对路径

[英]Relative paths using javascript in different browsers

I am using JavaScript to open a particular page relative to the current directory that I am in. The directory structure is: 我正在使用JavaScript打开相对于我当前所在目录的特定页面。目录结构为:

a/b/c/firstpage.html

If, I am currently in firstpage.html and I want to navigate to b/secondpage.html: 如果是,我目前在firstpage.html中,并且我想导航到b / secondpage.html:

in IE9/10 , I use the following code: IE9 / 10中 ,我使用以下代码:

../secondpage.html

While in Chrome , I have to use the following code: Chrome中 ,我必须使用以下代码:

./secondpage.html

because if I use 2 dots in Chrome it will go to a/secondpage.html 因为如果我在Chrome中使用2个点,它将转到a / secondpage.html

Can you please tell me how can I solve this to be compatible across all broswers? 您能告诉我如何解决所有浏览器兼容问题吗?

Use split and go two levels up in the current url, then concatenate your second url: 在当前网址中使用split并上移两个级别,然后连接第二个网址:

var loc = window.location.href.split('/').slice(0,-2).join('/');
var url = loc + '/secondpage.html';

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

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