首页 > 其他 > 详细

domReady方法(dom加载完成执行回调)

时间:2016-09-24 00:33:39      阅读:311      评论:0      收藏:0      [点我收藏+]
var domReady = function( fn ) {

    var isReady = false,

        ready = function(){
            if(!isReady){
                typeof fn === ‘function‘ && fn.call(document);
                isReady = true;
            }
        },

        DOMContentLoaded = function() {
            if ( document.addEventListener ) {
                document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
                ready();
            } else if ( document.readyState === "complete" ) {
                document.detachEvent( "onreadystatechange", DOMContentLoaded );
                ready();
            }
        };
    
    if ( document.readyState === "complete" ) {
        setTimeout( ready, 1 );
    } else if ( document.addEventListener ) {
        document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
        window.addEventListener( "load", ready, false );
    } else {
        document.attachEvent( "onreadystatechange", DOMContentLoaded );
        window.attachEvent( "onload", ready );


        var top = false;

        try {
            top = window.frameElement == null && document.documentElement;
        } catch(e) {}

        if ( top && top.doScroll ) {
            (function doScrollCheck() {
                if ( !isReady ) {
                    try {
                        top.doScroll("left");
                    } catch(e) {
                        return setTimeout( doScrollCheck, 50 );
                    }

                    ready();
                }
            })();
        }
    }
};


//调用
domReady(function(){
    alert(this.getElementById(‘div1‘).innerHTML);
});

 

domReady方法(dom加载完成执行回调)

原文:http://www.cnblogs.com/gongshunkai/p/5902038.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!