Javascript 相对于文件的路径

发布于 2024-11-01 19:58:17 字数 709 浏览 4 评论 0原文

我编写了一个 Web 应用程序,其中 JS 文件对其他文件进行了大量相对调用。到目前为止,这已经成功了,但现在我正在不同目录中的页面使用这个 js 文件,并且路径出现错误。这个网络应用程序将安装在许多不同的主机上,所以我不知道绝对路径是否有效,因为我不知道它安装在哪里。我也不想在路径中使用大量前置变量来破坏 js(但可能必须这样做)。

我希望 js 路径相对于 js 文件本身,而不是相对于打开它的文档...有没有办法在 js 文件中设置默认根目录?

$.ajax({
    type: "POST",
    url: "inc/alert.php",
    success: function(msg){
        if(msg){
            window.location.href = 'inc/logoff.php?timeout=true';
         }
    }
});

这是我的文件结构,但我不知道它将安装在哪里,也不知道 webapp 文件夹将被称为什么:

/random-folder/web-app/inc/script.js

直到现在我使用 js 的所有文件都在“web-app”文件夹中...但现在我有其他页面在不同的文件夹中调用它,并且所有路径都已损坏。有办法做到这一点吗?或者至少有一种方法可以获取js文件本身的路径

预先感谢您的帮助...

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).

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?

$.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:

/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?

Thanks in advance for any help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

纸短情长 2024-11-08 19:58:17

不幸的是,这并不容易实现。

但是,您可以尝试在 HTML 文档中查找


但是,由于您正在调用 PHP 脚本(显然不属于 JS 文件夹),因此您的问题根本不应该成为问题。您的 AJAX 调用将与包含脚本的文档相关,并且您可能知道该文档中 PHP 脚本的路径。

更新:只需阅读不同文件夹中的文件正在使用这些脚本。在这种情况下,如果您不想简单地手动设置包含路径的 var,则脚本标记解析技术实际上是可行的方法。

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.href you can then build an absolute URL to the script.


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.

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.

摘星┃星的人 2024-11-08 19:58:17

您可以通过检查当前文档中

HTML:

<script src="/some/random/path/main.js"></script>

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;
    }
});

You can find the JS file's path by checking the src attribute on its <script> tag in the current document.

HTML:

<script src="/some/random/path/main.js"></script>

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;
    }
});
顾挽 2024-11-08 19:58:17

不可能获得绝对路径,原因有两个:

  • JS 运行在客户端
  • JS 路径总是相对于:
    • HTML 页面(xyz.com/test/index.php 中的 scripts.js => xyz.com/test/scripts.js< /代码>)
    • 服务器文档根目录(xyz.com/test/index.php 中的/scripts.js => xyz.com/scripts.js< /代码>)

作为解决方案,您可能需要:

  • "scripts.php">
  • 或者,在加载脚本文件之前,加载与选项 1 相同的配置文件,例如:
    <脚本类型=“text/javascript”src=“config.php”><脚本类型=“text/javascript”src=“scripts.js”> ;

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.js in xyz.com/test/index.php => xyz.com/test/scripts.js)
    • The server document root (/scripts.js in xyz.com/test/index.php => xyz.com/scripts.js)

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文