在Javascript中的相对路径在一个外部文件 [英] Relative Paths in Javascript in an external file

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

问题描述

所以我跑这个JavaScript,一切工作正常,但该路径的背景图像。它适用于我的本地ASP.NET开发环境,但是当一个虚拟目录部署到服务器这是行不通的。

So I'm running this javascript, and everything works fine, except the paths to the background image. It works on my local ASP.NET Dev environment, but it does NOT work when deployed to a server in a virtual directory.

这是在外部.js文件,文件夹结构

This is in an external .js file, folder structure is

Site/Content/style.css
Site/Scripts/myjsfile.js
Site/Images/filters_expand.jpg
Site/Images/filters_colapse.jpg

那么这就是js文件是从

then this is where the js file is included from

Site/Views/ProductList/Index.aspx

$("#toggle").click(function() {
    if (left.width() > 0) {
        AnimateNav(left, right, 0);
        $(this).css("background", "url('../Images/filters_expand.jpg')");
    }
    else {
        AnimateNav(left, right, 170);
        $(this).css("background", "url('../Images/filters_collapse.jpg')");
    }
});

我试过使用/图片/ filters_collapse.jpg 并且不工作,要么;然而,它似乎在服务器上的工作,如果我使用../../图像/ filters_collapse.jpg

I've tried using '/Images/filters_collapse.jpg' and that doesn't work either; however, it seems to work on the server if I use '../../Images/filters_collapse.jpg'.

基本上,我想有相同的functionallity作为ASP.NET蒂尔达 -

Basically, I want have the same functionallity as the ASP.NET tilda -- ~.

更新

在外部的.js文件的路径相对于它们包含在页面中,或.js文件的实际位置文件?

Are paths in external .js files relative to the Page they are included in, or the actual location of the .js file?

推荐答案

在脚本中,路径是相对于显示的页面

,使事情变得简单,你可以打印出一个简单的JS声明,像这样和使用所有跨脚本这个变量:

to make things easier you can print out a simple js declaration like this and using this variable all across your scripts:

解决方案,这是受雇于计算器围绕2010年2月:

Solution, which was employed on StackOverflow around Feb 2010:

<script type="text/javascript">
   var imagePath = 'http://sstatic.net/so/img/';
</script>

如果你访问2010年左右这个页面你可以只看看计算器的HTML源代码后,你会发现这个坏蛋单行[格式化〜3行:)]在&LT;头/&GT; 部分

If you were visiting this page around 2010 you could just have a look at StackOverflow's html source, you could find this badass one-liner [formatted to 3 lines :) ] in the <head /> section

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

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