检查是否已经加载jquery,然后加载它,如果为false

有没有人知道如何检查,如果jQuery已被加载(与JavaScript),然后加载它,如果没有被加载。

就像是

if(!jQuery) { //load jquery file } 

也许这样的事情:

 <script> if(!window.jQuery) { var script = document.createElement('script'); script.type = "text/javascript"; script.src = "path/to/jQuery"; document.getElementsByTagName('head')[0].appendChild(script); } </script> 

避免使用“if(!jQuery)”,因为IE会返回错误:jQuery是'undefined'

而是使用:if(typeof jQuery =='undefined')

 <script type="text/javascript"> if (typeof jQuery == 'undefined') { var script = document.createElement('script'); script.type = "text/javascript"; script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"; document.getElementsByTagName('head')[0].appendChild(script); } </script> 

您还需要检查JQuery是否在将其追加到标题后加载。 否则,你将不得不等待window.onload事件,如果页面有图像,则会比较慢。 这里有一个示例脚本,检查JQuery文件是否已经加载,因为你不会有方便的使用$(document).ready(function …

http://neighborhood.org/core/sample/jquery/append-to-head.htm

方法1:

 if (window.jQuery) { // jQuery is loaded } else { // jQuery is not loaded } 

方法2:

 if (typeof jQuery == 'undefined') { // jQuery is not loaded } else { // jQuery is loaded } 

如果jquery.js文件没有加载,我们可以像这样强制加载:

 if (!window.jQuery) { var jq = document.createElement('script'); jq.type = 'text/javascript'; // Path to jquery.js file, eg. Google hosted version jq.src = '/path-to-your/jquery.min.js'; document.getElementsByTagName('head')[0].appendChild(jq); } 

尝试这个 :

 <script> window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>') </script> 

这将检查jQuery是否可用,如果不是,它将从指定的pathdynamic添加一个。

参考: 模拟jQuery的“include_once”

要么

include_once相当于js。 Ref: https : //raw.github.com/kvz/phpjs/master/functions/language/include_once.js

 function include_once (filename) { // http://kevin.vanzonneveld.net // + original by: Legaev Andrey // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: Michael White (http://getsprink.com) // + input by: Brett Zamir (http://brett-zamir.me) // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + bugfixed by: Brett Zamir (http://brett-zamir.me) // - depends on: include // % note 1: Uses global: php_js to keep track of included files (though private static variable in namespaced version) // * example 1: include_once('http://www.phpjs.org/js/phpjs/_supporters/pj_test_supportfile_2.js'); // * returns 1: true var cur_file = {}; cur_file[this.window.location.href] = 1; // BEGIN STATIC try { // We can't try to access on window, since it might not exist in some environments, and if we use "this.window" // we risk adding another copy if different window objects are associated with the namespaced object php_js_shared; // Will be private static variable in namespaced version or global in non-namespaced // version since we wish to share this across all instances } catch (e) { php_js_shared = {}; } // END STATIC if (!php_js_shared.includes) { php_js_shared.includes = cur_file; } if (!php_js_shared.includes[filename]) { if (this.include(filename)) { return true; } } else { return true; } return false; } 

即使你可能有一个头追加它可能不适用于所有的浏览器。 这是我发现一致工作的唯一方法。

 <script type="text/javascript"> if (typeof jQuery == 'undefined') { document.write('<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"><\/script>'); } </script> 
 var f = ()=>{ if (!window.jQuery) { var e = document.createElement('script'); e.src = "jquery-3.2.1.min.js"; e.onload = function () { jQuery.noConflict(); console.log('jQuery ' + jQuery.fn.jquery + ' injected.'); }; document.head.appendChild(e); } else { console.log('jQuery ' + jQuery.fn.jquery + ''); } }; f(); 

我正在使用CDN作为我的项目,并作为回退处理的一部分,我使用下面的代码,

 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript"> if ((typeof jQuery == 'undefined')) { document.write(unescape("%3Cscript src='/Responsive/Scripts/jquery-1.9.1.min.js' type='text/javascript'%3E%3C/script%3E")); } </script> 

只是为了validation,我删除了CDN引用并执行代码。 它的破碎,它从来没有进入如果循环作为typeof jQuery来作为函数而不是未定义。

这是因为jquery 1.6.1的caching旧版本,它返回函数并打破我的代码,因为我使用的是jquery 1.9.1。 因为我需要精确版本的jQuery,我修改了代码如下,

 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript"> if ((typeof jQuery == 'undefined') || (jQuery.fn.jquery != "1.9.1")) { document.write(unescape("%3Cscript src='/Responsive/Scripts/jquery-1.9.1.min.js' type='text/javascript'%3E%3C/script%3E")); } </script>