Javascript 相对于文件的路径
我编写了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,这并不容易实现。
但是,您可以尝试在 HTML 文档中查找
标记并解析其位置。然后,您可以与
location.href
一起构建脚本的绝对 URL。但是,由于您正在调用 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 withlocation.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.
您可以通过检查当前文档中
标记上的
src
属性来找到 JS 文件的路径。HTML:
jQuery:
You can find the JS file's path by checking the
src
attribute on its<script>
tag in the current document.HTML:
jQuery:
不可能获得绝对路径,原因有两个:
xyz.com/test/index.php
中的scripts.js
=>xyz.com/test/scripts.js< /代码>)
xyz.com/test/index.php
中的/scripts.js
=>xyz.com/scripts.js< /代码>)
作为解决方案,您可能需要:
<脚本类型=“text/javascript”src=“config.php”><脚本类型=“text/javascript”src=“scripts.js”> ;
It is not possible to get the absolute path, for two reasons:
scripts.js
inxyz.com/test/index.php
=>xyz.com/test/scripts.js
)/scripts.js
inxyz.com/test/index.php
=>xyz.com/scripts.js
)As a solution, you might want to:
<script type="text/javascript" src="scripts.php"></script>
<script type="text/javascript" src="config.php"></script><script type="text/javascript" src="scripts.js"></script>