B T*_*B T 9 javascript ajax jquery load include
所以我正在尝试动态加载脚本并找出加载该脚本的URL路径.如果脚本是静态加载的(如何获取当前正在执行的javascript代码的文件路径),有些人给了我一个非常棒的解决方案来解决这个问题.但我需要一个动态加载的解决方案.例如:
$(function()
{ $.getScript("brilliant.js", function(data, textStatus)
{ // do nothing
});
});
Run Code Online (Sandbox Code Playgroud)
"brilliant.js"在哪里:
var scripts = document.getElementsByTagName("script");
var src = scripts[scripts.length-1].src;
alert("THIS IS: "+src);
Run Code Online (Sandbox Code Playgroud)
理想情况下,这应该打印出"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);
});
}
}
Run Code Online (Sandbox Code Playgroud)
当您的脚本加载了jQuery(我猜其他框架也是如此)时,您的脚本将与最初在HTML文档中的脚本无法区分.
jQuery向您的脚本发出请求,并将回复作为<script>节点的文本子项放回.您的浏览器无法知道它的来源,是否在插入之前进行了修改等.就她而言,它只是一个脚本节点.
但是,可以有解决方法.在jQuery的情况下,您可以连接到ajax事件并利用在脚本执行后立即调用它们的事实.基本上,这将在您的示例中产生"brilliant.js":
var handler = function (e, xhr, s) {
alert(s.url);
}
$(document).ajaxSuccess(handler);
Run Code Online (Sandbox Code Playgroud)
更精细的一个:
(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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7970 次 |
| 最近记录: |