check if jquery has been loaded, then load it if false

是否有人知道如何检查 jquery 是否已经加载(使用 javascript) ,然后加载它,如果它没有被加载。

比如

if(!jQuery) {
//load jquery file
}
154736 次浏览

也许是这样的:

<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 是“未定义的”

而是使用: if (typeof jQuery = = ‘ unDefinition’)

<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>

You'll also need to check if the JQuery has loaded after appending it to the header. Otherwise you'll have to wait for the window.onload event, which is slower if the page has images. Here's a sample script which checks if the JQuery file has loaded, since you won't have the convenience of being able to use $(document).ready(function...

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

试试这个:

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

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

档号: 模拟 jQuery 的“ include _ once”

OR

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>

方法一:

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

Method 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);
}

我使用 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>

Just to verify, i removed CDN reference and execute the code. Its broken and it's never entered into if loop as typeof jQuery is coming as 功能 instead of undefined .

这是因为缓存了旧版本的 jquery 1.6.1,它返回 功能并破坏了我的代码,因为我使用的是 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>
var f = ()=>{
if (!window.jQuery) {
var e = document.createElement('script');
e.src = "https://code.jquery.com/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();

您可以通过以下方法检查 jQuery 是否已加载:

if (typeof jQuery == 'undefined') {


// jQuery IS NOT loaded, do stuff here.


}


if (typeof jQuery == 'function')
//or
if (typeof $== 'function')


if (jQuery) {
// This will throw an error in STRICT MODE if jQuery is not loaded, so don't use if using strict mode
alert("jquery is loaded");
} else {
alert("Not loaded");
}


if( 'jQuery' in window ) {
// Do Stuff
}

现在,在检查 jQuery 是否未加载之后,您可以这样加载 jQuery:

虽然这一部分已经被许多人在这篇文章中回答了,但是仍然 为了代码

的完整性而回答


    // This part should be inside your IF condition when you do not find jQuery loaded
var script = document.createElement('script');
script.type = "text/javascript";
script.src = "http://code.jquery.com/jquery-3.3.1.min.js";
document.getElementsByTagName('head')[0].appendChild(script);

老职位,但我做了一个很好的解决方案,是在几个地方测试。

Https://github.com/creativform/load-jquery-if-it-is-not-already-loaded

密码:

(function(url, position, callback){
// default values
url = url || 'https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js';
position = position || 0;


// Check is jQuery exists
if (!window.jQuery) {
// Initialize <head>
var head = document.getElementsByTagName('head')[0];
// Create <script> element
var script = document.createElement("script");
// Append URL
script.src = url;
// Append type
script.type = 'text/javascript';
// Append script to <head>
head.appendChild(script);
// Move script on proper position
head.insertBefore(script,head.childNodes[position]);


script.onload = function(){
if(typeof callback == 'function') {
callback(jQuery);
}
};
} else {
if(typeof callback == 'function') {
callback(jQuery);
}
}
}('https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js', 5, function($){
console.log($);
}));

在 GitHub 上的解释更好,但通常这个函数可以添加到 HTML 代码中的任何位置,如果还没有加载,那么将初始化 jquery。

<script>
if (typeof(jQuery) == 'undefined'){
document.write('<scr' + 'ipt type="text/javascript" src=" https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></scr' + 'ipt>');
}
</script>