javascript-如果未加载,则加载jQuery
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-如果未加载,则加载jQuery,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2125字,纯文字阅读大概需要4分钟。
内容图文
![javascript-如果未加载,则加载jQuery](/upload/InfoBanner/zyjiaocheng/682/eba54952d95c4f87878b565703b4183f.jpg)
我创建了以下格式的jQuery插件.在某些站点上,我注意到收到诸如TypeError之类的错误消息:$.myApp不是函数.
我还注意到,其中一些网站可能不止一次包含jQuery,而其他插件可能包含jQuery的版本,或者甚至根本没有加载jQuery.我对这些网站或正在使用的其他插件没有任何控制权.
我如何确保我的插件可靠运行,而不考虑安装了其他什么jQuery插件和版本?
;(function($, window, document, undefined) {
var myApp = function(options) {
this.init = function() {
};
this.init();
};
$.myApp = function(options) {
return myApp (options);
};
})(jQuery, window, document);
解决方法:
您可以使用Ben Alman的Bookmarklet generator(code source)中的代码:
(function( window, document, req_version, callback, $, script, done, readystate ){
// If jQuery isn't loaded, or is a lower version than specified, load the
// specified version and call the callback, otherwise just call the callback.
if ( !($= window.jQuery) || req_version > $.fn.jquery || callback( $) ) {
// Create a script element.
script = document.createElement( 'script' );
script.type = 'text/javascript';
// Load the specified jQuery from the Google AJAX API server (minified).
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/' + req_version + '/jquery.min.js';
// When the script is loaded, remove it, execute jQuery.noConflict( true )
// on the newly-loaded jQuery (thus reverting any previous version to its
// original state), and call the callback with the newly-loaded jQuery.
script.onload = script.onreadystatechange = function() {
if ( !done && ( !( readystate = this.readyState )
|| readystate == 'loaded' || readystate == 'complete' ) ) {
callback( ($= window.jQuery).noConflict(1), done = 1 );
$( script ).remove();
}
};
// Add the script element to either the head or body, it doesn't matter.
document.documentElement.childNodes[0].appendChild( script );
}
})( window, document,
// Minimum jQuery version required. Change this as-needed.
'1.3.2',
// Your jQuery code goes inside this callback. $refers to the jQuery object,
// and L is a boolean that indicates whether or not an external jQuery file
// was just "L"oaded.
function( $, L ) {
'$:nomunge, L:nomunge'; // Used by YUI compressor.
/* YOUR JQUERY CODE GOES HERE */
}
);
内容总结
以上是互联网集市为您收集整理的javascript-如果未加载,则加载jQuery全部内容,希望文章能够帮你解决javascript-如果未加载,则加载jQuery所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。