相对于文件的 Javascript 路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5699606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript paths relative to file
提问by RANGER
I have written a web app where the JS file lots of relative calls to other files. This has worked up to this point but now I am using this js file from pages in different directories and I am getting errors with the paths. This web app will be installed on lots of different hosts so I don't know if absolute paths will work as I won't know where it is installed. I also don't want to junk up the js with lots of prepended variables in my paths (but might have to).
我编写了一个 Web 应用程序,其中 JS 文件对其他文件进行了大量相关调用。到目前为止,这已经奏效了,但是现在我正在使用来自不同目录中的页面的这个 js 文件,并且路径出现错误。这个网络应用程序将安装在许多不同的主机上,所以我不知道绝对路径是否可以工作,因为我不知道它的安装位置。我也不想在我的路径中使用大量前置变量来破坏 js(但可能不得不这样做)。
I would LIKE the js paths to be relative to the js file itself and not the document opening it... is there a way to set a default root directory within a js file?
我希望js路径相对于js文件本身而不是打开它的文档......有没有办法在js文件中设置默认根目录?
$.ajax({
type: "POST",
url: "inc/alert.php",
success: function(msg){
if(msg){
window.location.href = 'inc/logoff.php?timeout=true';
}
}
});
This is my file structure, but I don't know where it will be installed or what the webapp folder will be called:
这是我的文件结构,但我不知道它将安装在哪里或将调用 webapp 文件夹:
/random-folder/web-app/inc/script.js
/random-folder/web-app/inc/script.js
And until now all my files using the js were in the "web-app" folder... but now I have other pages calling it in different folders and all of the paths are broken. Is there a way to do this? Or is there at least a way to get the path of the js file itself?
直到现在我所有使用 js 的文件都在“web-app”文件夹中......但现在我有其他页面在不同的文件夹中调用它并且所有路径都被破坏了。有没有办法做到这一点?或者至少有一种方法可以获取 js 文件本身的路径?
Thanks in advance for any help...
在此先感谢您的帮助...
回答by ThiefMaster
Unfortunately this is not easily possible.
不幸的是,这并不容易。
However, you could try finding the <script>tag in the HTML document and parse its location. Together with location.hrefyou can then build an absolute URL to the script.
但是,您可以尝试<script>在 HTML 文档中查找标记并解析其位置。location.href然后可以与您一起构建脚本的绝对 URL。
However, since you are calling PHP scripts - which clearly do not belong into the JS folder - your problem shouldn't be an issue at all. Your AJAX calls will be relative to the document containing the scripts and you probably know the path to your PHP scripts from that document.
但是,由于您正在调用 PHP 脚本 - 显然不属于 JS 文件夹 - 您的问题根本不应该成为问题。您的 AJAX 调用将与包含脚本的文档相关,您可能知道该文档中 PHP 脚本的路径。
Update: Just read that files from different folders are using those scripts. In this case the script tag parsing technique is actually the way to go if you don't want to simply set a var containing the path manually.
更新:只需阅读来自不同文件夹的文件正在使用这些脚本。在这种情况下,如果您不想简单地手动设置包含路径的 var,则脚本标记解析技术实际上是可行的方法。
回答by Thomas Higginbotham
You can find the JS file's path by checking the srcattribute on its <script>tag in the current document.
您可以通过检查当前文档中src其<script>标签上的属性来找到 JS 文件的路径。
HTML:
HTML:
<script src="/some/random/path/main.js"></script>
jQuery:
jQuery:
var scriptFilename = 'main.js';
var rootPath = ''; // Will contain the path to your file
$('script').each(function() {
var $script = $(this);
if ($script.attr('src') && $script.attr('src').indexOf(scriptFilename) > -1) {
rootPath = $script.attr('src').split(scriptFilename)[0];
return false;
}
});
回答by Christian
It is not possible to get the absolute path, for two reasons:
不可能得到绝对路径,原因有二:
- JS is run on the client side
- The JS path is always relative to:
- The HTML page (
scripts.jsinxyz.com/test/index.php=>xyz.com/test/scripts.js) - The server document root (
/scripts.jsinxyz.com/test/index.php=>xyz.com/scripts.js)
- The HTML page (
- JS在客户端运行
- JS 路径总是相对于:
- HTML 页面(
scripts.js在xyz.com/test/index.php=> 中xyz.com/test/scripts.js) - 服务器文档根目录(
/scripts.js在xyz.com/test/index.php=> 中xyz.com/scripts.js)
- HTML 页面(
As a solution, you might want to:
作为解决方案,您可能希望:
- Either load your JS from a PHP, eg:
<script type="text/javascript" src="scripts.php"></script> - Or, prior to loading your scripts file, load a config file same as option 1, eg:
<script type="text/javascript" src="config.php"></script><script type="text/javascript" src="scripts.js"></script>
- 从 PHP 加载您的 JS,例如:
<script type="text/javascript" src="scripts.php"></script> - 或者,在加载脚本文件之前,加载与选项 1 相同的配置文件,例如:
<script type="text/javascript" src="config.php"></script><script type="text/javascript" src="scripts.js"></script>

