`

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();
  }
})();
分享到:
评论

相关推荐

    python-trick

    Python-trick,上传的事pdf文档

    S-a-D-trick.rar_Join In

    split file and join write in vb6

    深度学习领域CNN橄榄球比赛NFL目标检测(带数据集)-cnn-baseline-more-tta-trick

    语言:python 内容包括:源码、数据集、数据集描述、论文 目的:使用CNN算法在橄榄球比赛中目标检测。 带数据集很好运行,主页有搭建环境过程。主页有更多源码。 数据集描述如下: 在这场比赛中,你的任务是预测球员...

    tips-n-trick

    概述 这是Tips-N-Trick,书签等面向公众的文档。

    gtg-grind-trick-generator

    gtg-grind-trick-generator PWA Web应用程序(Node.js,JS,HTML,CSS) Chrome,Safari,Firefox,Edge(Android,iOS,MacOS,Windows) 离线工作Android应用程式使用Google Workbox,Webpack制作

    chinese-nlp-ner-master_chinese_BLSTM_

    一套针对中文实体识别的BLSTM-CRF解决方案,主要包括:数据处理,模型构建,模型训练,模型测试,服务部署(thrift和flask)两种方式。

    py-trick-book:关于 Python 的高级提示和技巧

    Python 提示和技巧 ... git clone https://github.com/plasmashadow/py-trick-book.git 我假设您安装了 ipython 导航到目录并执行 ipython notebook 笔记: 退出间谍活动并自己阅读 或者 保持冷静,加入草帽海贼团

    21-card-trick:在 React 中完成的 21 张卡片技巧

    React 21 卡技巧一个演示卡片技巧的React应用程序。动机该项目旨在学习如何使用 React 钩子和进行嵌套的 api 调用。 该项目不再进行。怎么玩记住 21 张卡片中的 1 张后,单击完成。 选择您的卡片所在的 3 堆中的哪一...

    HLP-Trick-crx插件

    语言:English (United States) 该扩展程序重新启用了网页上的复制/粘贴功能,上下文菜单和本机突出显示功能。

    CMCC-Trick:招惹CMCC-* WLAN

    Trick-CMCC 利用CMCC公共热点的小漏洞免费上网~~ :) sudo ./conn.sh Notice: 目前只知道我工CMCC有这特色, 其他地区尚不明确 Notice: 脚本适用于使用NetWorkManager网络sds管理工具的系统 Notice: 不必惊讶原理, ...

    HLP-绝招「HLP-Trick」-crx插件

    该扩展程序重新启用网页上的复制/粘贴功能,上下文菜单和本机突出显示功能。 支持语言:English (United States)

    Trick

    Trick

    [Head.First.JavaScript].Michael.Morrison...

    Use the [removed] tag to tell the browser you’re writing JavaScript 11 Your web browser can handle HTML, CSS, AND JavaScript 12 Man’s virtual best friend... needs YOUR help 15 Making iRock ...

    Pro Silverlight4 in C#.pdf

    Create cross-platform .NET applications for the browser Silverlight is a framework for building rich, browser-hosted applications that run on a variety of operating systems. Silverlight works its ...

    vdr-hattrick-开源

    vdr-hattrick是视频磁盘录像机(VDR)的插件。 它显示了在线足球游戏“ hattrick”(http://www.hattrick.org/)提供的实时比赛。

    css-skill.zip

    css-trick css常用的开发技巧

    Pro Silverlight2 in CSharp 2008

    to a web page that includes some Silverlight content, this browser plug-in runs, executes the code, and renders that content in a specifically designated region of the page. The important part is that...

    Hattrick球场上座率概算

    Hattrick球场上座率概算 Hattrick球场上座率概算

    The Kernel Trick.pdf

    机器学习中为什么需要 Kernel Trick 以及 Kernel Trick 的证明。

    arp_trick.tar.gz

    arp_trick.tar.gz

Global site tag (gtag.js) - Google Analytics