動(dòng)態(tài)加載jQuery的兩種方法實(shí)例分析
更新時(shí)間:2015年07月17日 15:29:20 作者:優(yōu)雅先生
這篇文章主要介紹了動(dòng)態(tài)加載jQuery的兩種方法,實(shí)例分析了jquery動(dòng)態(tài)加載的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了動(dòng)態(tài)加載jQuery的兩種方法。分享給大家供大家參考。具體如下:
第一種方法參考本站之前有人發(fā)的代碼,增加了加載檢測;
第二種方法來自去年的12306刷票腳本。
第一種方法:
function withjQuery(callback) {
if(!(window.jQuery)) {
var js = document.createElement('script');
js.setAttribute('src', 'https://dynamic.12306.cn/otsweb/js/common/jquery-1.4.2.min.js?version=5.47');
js.setAttribute('type', 'text/javascript');
js.onload = js.onreadystatechange = function() {
if (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete') {
if(callback && typeof callback === "function") {
callback();
}
js.onload = js.onreadystatechange = null;
}
};
document.getElementsByTagName('head')[0].appendChild(js);
}
}
withjQuery(
function() {
$(function(){ alert("jQuery loaded"); })();
}
);
第二種方法:
// ==UserScript==
// @name 12306 Booking Assistant
// @version 1.4.0
// @author zzdhidden@gmail.com
// @namespace https://github.com/zzdhidden
// @description 12306 訂票助手之(自動(dòng)登錄,自動(dòng)查票,自動(dòng)訂單)
// @include *://dynamic.12306.cn/otsweb/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
// ==/UserScript==
function withjQuery(callback, safe){
if(typeof(jQuery) == "undefined") {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
if(safe) {
var cb = document.createElement("script");
cb.type = "text/javascript";
cb.textContent = "jQuery.noConflict();(" + callback.toString() + ")(jQuery, window);";
script.addEventListener('load', function() {
document.head.appendChild(cb);
});
}
else {
var dollar = undefined;
if(typeof($) != "undefined") dollar = $;
script.addEventListener('load', function() {
jQuery.noConflict();
$ = dollar;
callback(jQuery, window);
});
}
document.head.appendChild(script);
} else {
setTimeout(function() {
//Firefox supports
callback(jQuery, typeof unsafeWindow === "undefined" ? window : unsafeWindow);
}, 30);
}
}
withjQuery(function($, window){
$(function() { alert("jQuery loaded"); })();
}, true);
希望本文所述對大家的jquery程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- 動(dòng)態(tài)加載jQuery的方法
- jQuery結(jié)合ajax實(shí)現(xiàn)動(dòng)態(tài)加載文本內(nèi)容
- jQuery實(shí)現(xiàn)頁面滾動(dòng)時(shí)動(dòng)態(tài)加載內(nèi)容的方法
- 使用jquery動(dòng)態(tài)加載js文件的方法
- 一個(gè)簡單的動(dòng)態(tài)加載js和css的jquery代碼
- 三種動(dòng)態(tài)加載js的jquery實(shí)例代碼另附去除js方法
- 動(dòng)態(tài)加載jquery庫的方法
- jquery Tab效果和動(dòng)態(tài)加載的簡單實(shí)例
- jquery動(dòng)態(tài)加載select下拉框示例代碼
相關(guān)文章
jquery統(tǒng)計(jì)輸入文字的個(gè)數(shù)并對其進(jìn)行判斷
判斷輸入文字個(gè)數(shù)并提示還可以輸入多少個(gè)字,類似的功能使用jquery便可輕松實(shí)現(xiàn)2014-01-01
基于jQuery模擬實(shí)現(xiàn)淘寶購物車模塊
這篇文章主要介紹了如何利用jQuery+css+html模擬實(shí)現(xiàn)淘寶購物車模塊,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起動(dòng)手嘗試一下2022-03-03
jQuery的實(shí)現(xiàn)原理的模擬代碼 -4 重要的擴(kuò)展函數(shù) extend
在上兩篇文章中,我們看到每次要通過 jQuery 的原型增加共享方法的時(shí)候,都需要通過 jQuery.fn 一個(gè)個(gè)進(jìn)行擴(kuò)展,非常麻煩.2010-08-08

