DOMContentLoaded兼容IE9以下版本

jQuery的实现

  // Mozilla, Opera and webkit nightlies currently support this event
      if ( document.addEventListener ) {
          // Use the handy event callback
          document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );

          // A fallback to window.onload, that will always work
          window.addEventListener( "load", jQuery.ready, false );

          // If IE event model is used
      } else if ( document.attachEvent ) {
          // ensure firing before onload,
          // maybe late but safe also for iframes
          document.attachEvent( "onreadystatechange", DOMContentLoaded );

          // A fallback to window.onload, that will always work
          window.attachEvent( "onload", jQuery.ready );

          // If IE and not a frame
          // continually check to see if the document is ready
          var toplevel = false;

          try {
              toplevel = window.frameElement == null;
          } catch(e) {}

          if ( document.documentElement.doScroll && toplevel ) {
              doScrollCheck();
          }
      }

        //函数DOMContentLoaded的赋值
        if ( document.addEventListener ) {
            DOMContentLoaded = function() {
                document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
                jQuery.ready();
            };

        } else if ( document.attachEvent ) {
            DOMContentLoaded = function() {
                // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
                if ( document.readyState === "complete" ) {
                    document.detachEvent( "onreadystatechange", DOMContentLoaded );
                    jQuery.ready();
                }
            };
        }

// The DOM ready check for Internet Explorer
        function doScrollCheck() {
            if ( jQuery.isReady ) {
                return;
            }

            try {
                // If IE is used, use the trick by Diego Perini
                // http://javascript.nwbox.com/IEContentLoaded/
                document.documentElement.doScroll("left");
            } catch(e) {
                setTimeout( doScrollCheck, 1 );
                return;
            }

            // and execute any waiting functions
            jQuery.ready();
        }

第二种实现方式

  // @win window reference
    // @fn function reference
    function contentLoaded(win, fn) {

        var done = false, top = true,

                doc = win.document,
                root = doc.documentElement,
                modern = doc.addEventListener,

                add = modern ? 'addEventListener' : 'attachEvent',
                rem = modern ? 'removeEventListener' : 'detachEvent',
                pre = modern ? '' : 'on',

                init = function(e) {
                    if (e.type == 'readystatechange' && doc.readyState != 'complete') {
                        return;
                    }

                    //load事件在win上,移除事件监听(readystatechange, DOMContentLoaded, load)
                    (e.type == 'load' ? win : doc)[rem](pre + e.type, init, false);

                    //保证fn只执行一次
                    if (!done && (done = true)) fn.call(win, e || e.type);
                },

                poll = function() {
                    try { root.doScroll('left'); } catch(e) { setTimeout(poll, 50); return; }
                    init('poll');
                };

        if (doc.readyState == 'complete') fn.call(win, 'lazy');
        else {
            if (!modern && root.doScroll) {
                try { top = !win.frameElement; } catch(e) { }
                if (top) poll();
            }
            doc[add](pre + 'DOMContentLoaded', init, false); //触发时,doc.readyState为'interactive'
            doc[add](pre + 'readystatechange', init, false); //会触发两次,readystatechange先触发,再触发DOMContentLoaded, 最后才是load
            win[add](pre + 'load', init, false);
        }

    }

你可能感兴趣的:(DOMContentLoaded兼容IE9以下版本)