如何检测动态加载时加载的jquery是否加载
How to detect jquery is loaded when loading it dynamicly
如果未加载,我正在尝试将jquery加载到页面。加载后,我将立即运行一些代码。使用下面的代码,我试图在加载 jquery 时捕获它。
我的代码正确加载了jquery,但没有引发onreadystatechange事件。
if(typeof jQuery=='undefined') {
var headTag = document.getElementsByTagName("head")[0];
var jqTag = document.createElement('script');
jqTag.type = 'text/javascript';
jqTag.src = 'http://localhost:8001/jquery-min.js';
headTag.appendChild(jqTag);
jqTag.onreadystatechange= function () {
if (this.readyState == 'complete') {
this.loadWidget(containerId);
}
};
}
else {
this.loadWidget(containerId);
}
加载 jquery 时如何捕获它?
脚本标记具有脚本加载时间的onload
事件
if (typeof jQuery === 'undefined') {
var headTag = document.getElementsByTagName("head")[0];
var jqTag = document.createElement('script');
jqTag.type = 'text/javascript';
jqTag.onload = function() {
this.loadWidget(containerId);
}
jqTag.src = 'http://localhost:8001/jquery-min.js';
headTag.appendChild(jqTag);
} else {
this.loadWidget(containerId);
}
在我看来,你的代码太复杂了。
为什么不做HTML5样板已经做过的事情:
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.2.min.js"><'/script>')</script>
如果未检测到 window.jQuery 对象,它会优雅地故障转移以写出脚本标记以将其包含在本机 Javascript 中。 当然,您可以将路径更改为您想要的任何路径。
希望这有帮助。
相关文章:
- 使用javascript函数在页面初始化后加载jquery
- 什么'这是加载jQuery的最佳方式
- 在WordPress站点中加载jquery.js后加载javascript代码
- 延迟加载jquery后替换$(document).ready(function)
- 如何使用onLoad加载jQuery JSON.从Zipcode On Form获取城市和州
- 对页面重新加载 Jquery 执行操作
- 在Ajax(过滤器)加载之后加载jQuery脚本
- 异步加载JQuery时Tabbed Box已损坏
- 加载Jquery时Chrome扩展出错
- 在更改事件上加载jQuery DataTable
- 手动加载JQuery而不触发内容安全策略
- 如果更改,请在ng之后重新加载Jquery
- 脚本似乎在加载jQuery之前运行-未定义Uncaught ReferenceError:$
- 在使用jquery之前动态加载jquery
- 如何重新加载jquery,以便它能为下一个进程工作
- Firefox没有't从ajax.googleapis.com加载jquery
- 我无法在自定义Wordpress主题中成功加载jQuery
- 正在从maphilight href加载Jquery选项卡
- 如何在完全加载html之后加载jquery代码段
- 在页面加载jQuery/Javascript时,按.class对表行进行排序