Javascript相对路径

时间:2015-10-11 07:50:06

标签: javascript json relative-path

在第一个html文件中,我使用变量 categoryLinks

var categoryLinks = {
    'Career prospects':'http://localhost/Landa/DirectManagers/511-HelenaChechik/Dim01.html',
    'Compensation':'http://localhost/Landa/DirectManagers/511-HelenaChechik/Dim02.html',....

在第二个html文件中,我通过拉一些Json创建一个图表:

$(function () {
    var chart1;
    $.get('graphdata/Dim1.txt?x='+microtime(), function(json){
    obj = eval('({'+json+'})');

在这个Json中有一行引用 categoryLinks 以指向相关的超链接:

labels: {
formatter: function () {
return '<a href="' + categoryLinks[this.value] + '">' +
this.value + '</a>';
},
style: {color: 'blue',textDecoration: 'underline',fontSize:'13px'} 
}

},

一切正常

我正在尝试将 categoryLinks 中的链接定义为相对路径,而不是绝对路径。我尝试过失败

var categoryLinks = {
    'Career prospects':'Landa/DirectManagers/511-HelenaChechik/Dim01.html',

1 个答案:

答案 0 :(得分:1)

这取决于您加载的网页的网址,您尚未展示,但您可能需要一个领先的/

var categoryLinks = {
    'Career prospects':'/Landa/DirectManagers/511-HelenaChechik/Dim01.html'
// Here ----------------^

......或可能是领先的../

var categoryLinks = {
    'Career prospects':'../Landa/DirectManagers/511-HelenaChechik/Dim01.html'
// Here ----------------^^^

..或类似。

基本上:

说你有

http://example.com/foo/page.html

并且你有一个相对链接:

testing/stuff.html

这将取代page.html,为您提供:

http://example.com/foo/testing/stuff.html

如果您不想进入foo,则可以使用前导/,这意味着“从域的根目录开始”:

/testing/stuff.html

...或..表示“升级”:

../testing/stuff.html

..可以多次使用,所以如果你有页面:

http://example.com/this/is/deeply/nested.html

相对链接

../../test.html

给你

http://example.com/this/test.html

另请注意,链接将与他们所在的HTML页面相关,他们所在的JavaScript脚本文件。

相关问题