JavaScript 相对路径 [英] Javascript relative path

查看:84
本文介绍了JavaScript 相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

var categoryLinks = {'职业前景':'http://localhost/Landa/DirectManagers/511-HelenaChechik/Dim01.html','补偿':'http://localhost/Landa/DirectManagers/511-HelenaChechik/Dim02.html',....

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

$(function () {无功图表1;$.get('graphdata/Dim1.txt?x='+microtime(), function(json){obj = eval('({'+json+'})');

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

标签:{格式化程序:函数(){返回 '​​'+this.value + '</a>';},样式:{颜色:'蓝色',textDecoration:'下划线',字体大小:'13px'}}

},

一切正常

我试图将 categoryLinks 中的链接定义为相对路径而不是绝对路径.我试过不成功

var categoryLinks = {'职业前景':'Landa/DirectManagers/511-HelenaChechik/Dim01.html',

这取决于您将它们加载到的页面的 URL,您尚未显示该 URL,但您可能需要一个前导 /:

var categoryLinks = {'职业前景':'/Landa/DirectManagers/511-HelenaChechik/Dim01.html'//这里----------------^

...或者可能是前导 ../:

var categoryLinks = {'职业前景':'../Landa/DirectManagers/511-HelenaChechik/Dim01.html'//这里----------------^^^

...或类似的.

基本上:

说你有

<前>http://example.com/foo/page.html

你有一个相对链接:

<前>测试/东西.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 脚本文件.

In a first html file, I use a variable categoryLinks :

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

In a second html file, I am creating a chart by pulling some Json:

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

In this Json there is a line that refers to categoryLinks in order to point to the related hyperlink:

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

},

All works well

I am trying to define the links in categoryLinks as a relative path and not an absolute one. I've tried unsuccessfully

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

解决方案

It depends on the URL of the page you're loading them into, which you haven't shown, but you may want a leading /:

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

...or possibly a leading ../:

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

..or similar.

Basically:

Say you have

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

and you have a relative link in it:

testing/stuff.html

That will replace page.html, giving you:

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

If you didn't want to be in foo, you'd either use a leading / which means "Start at the root of the domain":

/testing/stuff.html

...or .. which means "go up a level":

../testing/stuff.html

The .. can be used more than once, so if you have the page:

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

the relative link

../../test.html

gives you

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


Also note that the links will be relative to the HTML page they're in, not the JavaScript script file they're in.

这篇关于JavaScript 相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆