javascript - trick to cross browser DOM ready event

 

the "ready" event (implemented as DOMContentLoaded in W3C DOM-capable browsers). The ready event fires as soon as the entire DOM document has been loaded and is able to be traversed and manipulated. This event has become an integral part of many modern frameworks, allowing code to be layered in an unobtrusive manner, frequently executing before the page is displayed.

 

 

following .js file shows you how to do implement the cross-browser DOM ready events.

 

 

 

/**************************************
*@Name: documentready.js
*  "ready" event (implemented in DOMConententLoaded in W3C DOM-capable brwosers). 
*@Summary
*  The ready event fires as soon as the entire DOM document has been loaded and is able to be traversed and manipulated; allowing code to be layered in an unobtrusive manner,
*  frequently executing before the page is displayed.
*
* @todo:
*   Test and add them to the docs
***************************************/

(function () {
  var isReady = false, DOMContentLoaded;
  // Catch cases where addReady is called after the
  // browser event has already occurred.
  if (document.readyState === "complete") {
    return ready();
  }
  // Mozilla, Opera and Webkit currently support this event
  if (document.addEventListener) {
    DOMContentLoaded = function () {
      document.removeEventListener(
"DOMContentLoaded", DOMContentLoaded, false);
      ready();
    };
    // Use the handy event callback
    document.addEventListener(
"DOMContentLoaded", DOMContentLoaded, false);
    // If IE event model is used
  } else if (document.attachEvent) {
    DOMContentLoaded = function () {
      if (document.readyState === "complete") {
        document.detachEvent(
"onreadystatechange", DOMContentLoaded);
        ready();
      }
    };
    // ensure firing before onload,
    // maybe late but safe also for iframes -- this is the fallback of the doScrollCheck because the doScrollCheck works only in IE not in iframe
    document.attachEvent(
"onreadystatechange", DOMContentLoaded);
    // 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();
    }
  }
  function ready() {
    if (!isReady) {
      triggerEvent(document, "ready");
      isReady = true;
    }
  }
  // The DOM ready check for Internet Explorer
  // calling doScroll will have effect scrolling the viewPort to the left side 
  // but more importantly, it will throw an exception if the document is not ready 
  // however, it does not work in the iframe, so we have the fallback mentioned before
  function doScrollCheck() {
    if (isReady) {
      return;
    }
    try {
      // If IE is used, use the trick by Diego Perini
      // http://javascript.nwbox.com/IEContentLoaded/
      document.documentElement.doScroll("left");
    } catch (error) {
      setTimeout(doScrollCheck, 1);
      return;
    }
    // and execute any waiting functions
    ready();
  }
})();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值