มีใครรู้วิธีตรวจสอบว่ามีการโหลด jquery หรือไม่ (ด้วย javascript) จากนั้นโหลดหากยังไม่ได้โหลด
สิ่งที่ต้องการ
if(!jQuery) {
//load jquery file
}
มีใครรู้วิธีตรวจสอบว่ามีการโหลด 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>
head
ที่สามารถต่อท้ายองค์ประกอบสคริปต์กับ
( document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0] ).appendChild( script );
หลีกเลี่ยงการใช้ "if (! jQuery)" เนื่องจาก IE จะส่งคืนข้อผิดพลาด: jQuery คือ 'ไม่ได้กำหนด'
ใช้แทน: 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 event ซึ่งจะช้ากว่าถ้าหน้ามีภาพ นี่คือสคริปต์ตัวอย่างที่ตรวจสอบว่าไฟล์ JQuery โหลดหรือไม่เนื่องจากคุณจะไม่สะดวกในการใช้ $ (document) พร้อม (ฟังก์ชั่น ...
http://neighborhood.org/core/sample/jquery/append-to-head.htm
script.onload = function() { alert('jQuery loaded!'); }
? จะได้ผลหรือไม่?
วิธีที่ 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 พร้อมใช้งานหรือไม่หากไม่มีจะเพิ่มหนึ่งรายการแบบไดนามิกจากเส้นทางที่ระบุ
อ้างอิง: จำลอง "include_once" สำหรับ jQuery
หรือ
include_once เทียบเท่าสำหรับ js อ้างอิง: 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>
document.write
ขอขมวดคิ้ว?
คุณสามารถตรวจสอบว่า 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 หากยังไม่ได้โหลด
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();
<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>
ฉันใช้ 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>
เพื่อตรวจสอบฉันลบการอ้างอิง CDN และรันโค้ด มันเสียและไม่เคยเข้ามาใน if loop เนื่องจากtypeof jQueryกำลังมาเป็นฟังก์ชันแทนที่จะไม่ได้กำหนด
นี่เป็นเพราะ 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>