您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

动态加载 js css的类

2025/5/24 11:07:16发布12次查看
一个动态加载 js 和 css 的类
虽然很少用,但是还是用到了
自己改的/**
 * yloader
 * 动态加载 js css 
 * 用法
 *
 *  var yloader = new yloader();
 *
 *  1. 动态加载js
 *    yloader.use('http://xxx.com/static/js/jquery.js');
 *  2. 动态加载js后执行方法
 *    yloader.use('http://xxx.com/static/js/jquery.js', function(){...});
 *  3. 动态加载css
 *    yloader.use('http://xxx.com/static/css/home.css');
 */
function yloader() {
    this.doc = document;
    this.is_css_reg = /\.css(?:\?|$)/i;
    this.ready_state_reg = /^(?:loaded|complete|undefined)$/;
// bug fix
    // `onload` event is not supported in webkit     // ref:
    //  - https://bugs.webkit.org/show_activity.cgi?id=38995
    //  - https://bugzilla.mozilla.org/show_bug.cgi?id=185236
    //  - https://developer.mozilla.org/en/html/element/link#stylesheet_load_events
    this.isoldwebkit = (window.navigator.useragent.replace(/.*applewebkit\/(\d+)\..*/, $1)) * 1     // for some cache cases in ie 6-8, the script executes immediately after
    // the end of the insert execution, so use `currentlyaddingscript` to
    // hold current node
    this.currentlyaddingscript = '';
    this.head = this.doc.getelementsbytagname('head')[0];
    // ref: #185 & http://dev.jquery.com/ticket/2709
    this.baseelement = this.head.getelementsbytagname(base)[0];
}
yloader.prototype = {
    constructor : yloader
    ,isfunction : function(fn) {
        return [object function] === object.prototype.tostring.call(fn);
    }
    ,pollcss : function(node, callback) {
        var _self = this;
        var sheet = node.sheet;
        var isloaded = false;
// for webkit         if(_self.isoldwebkit) {
            if(sheet) {
                isloaded = true;
            }
        } else {
            if (sheet) {  // for firefox                 try {
                    if(sheet.cssrules) {
                        isloaded = true;
                    }
                } catch (ex) {
                    // the value of `ex.name` is changed from ns_error_dom_security_err
                    // to securityerror since firefox 13.0. but firefox is less than 9.0
                    // in here, so it is ok to just rely on ns_error_dom_security_err
                    if(ex.name === ns_error_dom_security_err) {
                        isloaded = true;
                    }
                }
            }
        }
settimeout(function() {
            if (isloaded) {
                // place callback here to give time for style rendering
                _self.isfunction(callback) && callback();
            } else {
                _self.pollcss(node, callback);
            }
        }, 50);
    }
    ,addonload : function(node, callback, iscss) {
        var _self = this;
        var missingonload = iscss && (_self.isoldwebkit || !(onload in node));
        // for old webkit and old firefox
        if(missingonload) {
            settimeout(function() {
                _self.pollcss(node, callback);
            }, 10);  // begin after node insertion
            return;
        }
node.onload = node.onerror = node.onreadystatechange = function() {
            if(_self.ready_state_reg.test(node.readystate)) {
                // ensure only run once and handle memory leak in ie
                node.onload = node.onerror = node.onreadystatechange = null;
                // remove the script to reduce memory leak
                if(!iscss) {
                    _self.head.removechild(node);
                }
                // dereference the node
                node = null;
                _self.isfunction(callback) && callback();
            }
        };
    }
    ,use : function(url, callback, charset) {
        var iscss = this.is_css_reg.test(url);
        var node = this.doc.createelement(iscss ? link : script);
        if(undefined != charset) {
            node.charset = charset;
        }
        this.addonload(node, callback, iscss);
        if (iscss) {
            node.rel = stylesheet;
            node.href = url;
        } else {
            node.async = true;
            node.src = url;
        }
        this.currentlyaddingscript = node;
// ref: #185 & http://dev.jquery.com/ticket/2709
        this.baseelement ?
            this.head.insertbefore(node, this.baseelement) :
            this.head.appendchild(node);
this.currentlyaddingscript = null;
    }
};
yloader.js.rar ( 1.64 kb 下载:20 次 )
ad:真正免费,域名+虚机+企业邮箱=0元
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product