动态加载时获取当前正在执行的js文件的URL

所以我正在尝试动态加载脚本并找出加载该脚本的URL路径。 如果脚本是静态加载的( 如何获取当前正在执行的javascript代码的文件路径),有些人给了我一个非常棒的解决方案来解决这个问题。 但我需要一个动态加载的解决方案。 例如:

$(function() { $.getScript("brilliant.js", function(data, textStatus) { // do nothing }); }); 

“brilliant.js”的地方:

 var scripts = document.getElementsByTagName("script"); var src = scripts[scripts.length-1].src; alert("THIS IS: "+src); 

理想情况下,这应该打印出“brilliant.js”或“⟨hostname+basepath⟩/ brilliant.js”

目前,brilliant.js适用于静态包含的脚本,但不适用于动态包含的脚本(如$ .getScript)。 有人有主意吗? dom中是否存储了所有已加载的脚本?

编辑:Andras提供了一个非常好的解决方案,虽然它可能只适用于jQuery。 因为那可能是最受欢迎的图书馆,而且我肯定会使用它。 它也可以扩展到其他库。 这是我的简化版本:

 var scriptUri; curScriptUrl(function(x) { scriptUri = x; alert(scriptUri); }); function curScriptUrl(callback) { var scripts = document.getElementsByTagName("script"); var scriptURI = scripts[scripts.length-1].src; if(scriptURI != "") // static include { callback(scriptURI); }else if($ != undefined) // jQuery ajax { $(document).ajaxSuccess(function(e, xhr, s) { callback(s.url); }); } } 

当您的脚本加载了jQuery(我猜其他框架也是如此)时,您的脚本将与最初在HTML文档中的脚本无法区分。

jQuery向您的脚本发出请求,并将回复作为

但是,可以有解决方法。 在jQuery的情况下,您可以连接到ajax事件并利用在脚本执行后立即调用它们的事实。 基本上,这将在您的示例中产生“brilliant.js”:

 var handler = function (e, xhr, s) { alert(s.url); } $(document).ajaxSuccess(handler); 

更精细的一个:

 (function ($, undefined) { /* Let's try to figure out if we are inlined.*/ var scripts = document.getElementsByTagName("script"); if (scripts[scripts.length - 1].src.length === 0) { // Yes, we are inlined. // See if we have jQuery loading us with AJAX here. if ($ !== undefined) { var initialized = false; var ajaxHandler = function (e, xhr, s) { if (!initialized) { initialized = true; alert("Inlined:" + s.url); initmywholejsframework(); } } //If it is, our handler will be called right after this file gets loaded. $(document).ajaxSuccess(ajaxHandler); //Make sure to remove our handler if we ever yield back. window.setTimeout(function () { jQuery(document).unbind("ajaxSuccess", ajaxHandler); if (!initialized) { handleInlinedNonjQuery(); } }, 0); } } else { //We are included. alert("Included:" + scripts[scripts.length - 1].src); initmywholejsframework(); } //Handle other JS frameworks etc. here, if you will. function handleInlinedNonjQuery() { alert("nonJQuery"); initmywholejsframework(); } //Initialize your lib here function initmywholejsframework() { alert("loaded"); } })(jQuery); 

BT,对不起,如果这没有帮助,但我很好奇为什么你需要这样做? 我问的原因是我不明白为什么你不能只使用相对文件路径来加载这些文件? 找到你所在的位置可以通过window.location来完成,但你为什么要这样做? 至于加载它们,你不能对文件进行ajax调用然后评估它们吗?

这将在除IE之外的每个浏览器中工作,并且不依赖于假设文件的名称是:

 var getErrorLocation = function (error) { var loc, replacer = function (stack, matchedLoc) { loc = matchedLoc; }; if ("fileName" in error) { loc = error.fileName; } else if ("stacktrace" in error) { // Opera error.stacktrace.replace(/Line \d+ of .+ script (.*)/gm, replacer); } else if ("stack" in error) { // WebKit error.stack.replace(/at (.*)/gm, replacer); loc = loc.replace(/:\d+:\d+$/, ""); // remove line number } return loc; }; try { 0(); } catch (e) { var scriptURI = getErrorLocation(e); } alert("THIS IS: " + scriptURI);