JavaScript中当前脚本的URL
How can I find out the URL of the JS file that was called?
如何查找被调用的JS文件的URL ?
Let's say the JS file (which is part of a library) and the HTML file (which is the application) are on a different server. To find out the URL of the HTML file is easy. But how can I find out the server name and path of the JS files' server within the JS file itself? (Edit: There is a loader which invokes other JS files of thsi library in the same directory structure)
假设JS文件(它是一个库的一部分)和HTML文件(应用程序)在不同的服务器上。查找HTML文件的URL很容易。但是如何在JS文件本身中找到JS文件服务器的服务器名和路径呢?(编辑:有一个加载程序在相同的目录结构中调用thsi库的其他JS文件)
Preferably a JQuery or pure JS solution.
最好是JQuery或纯JS解决方案。
Edit:
编辑:
After I learned from your answers and comments I decided it would be cleaner to actually move the loader code to the first server, where also the html lives, which avoids the problem.
在我从您的回答和评论中了解到之后,我决定将加载程序代码移动到第一个服务器(也就是html所在的服务器),这样可以避免这个问题。
2 个解决方案
#1
4
In JQuery you could do:
在JQuery中你可以做到:
$('script').each(function(i, e) {
alert($(e).attr('src'));
});
更多相关文章
- 如何使用Angular.js从json文件中使用id获取特定数据
- 如何从一个节点生成exe文件。js应用?
- 客户端处理和服务器端处理,哪个比较快?
- 在批处理文件中使用JSMin
- 使用HTML文件中的React调用.js文件中的Javascript函数
- 返回JsonResult会导致500内部服务器错误
- 在JavaScript中进行文件处理,第四部分:对象URLs
- [转]在网页中加入声音文件,并且用JavaScript对其进行播放控制
- 从服务器(任何服务器)获取当前日期和时间。仅限javascript