onDomLoaded

2007-07-25 20:50:58
Dean Edwards解决方案封装一下, 方便以后调用.
function onDomLoaded(func){
  function init() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;
    if (_timer) clearInterval(_timer);
    //do stuff
    func();
  };
  if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, false);
  }
  /*@cc_on @*/
  /*@if (@_win32)
    document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
    var script = document.getElementById("__ie_onload");
    script.onreadystatechange = function() {
      if (this.readyState == "complete") {
        init(); // call the onload handler
      }
    };
  /*@end @*/
  if (/WebKit/i.test(navigator.userAgent)) {
    var _timer = setInterval(function() {
      if (/loaded|complete/.test(document.readyState)) {
        init();
      }
    }, 10);
  }
  window.onload = init;
}
[Last Modified By atan, at 2007-07-27 08:37:33]
Comments Feed Comments Feed: http://www.atan.cn/feed.asp?q=comment&id=517

There is no comment on this article.

You can't post comment on this article.

Login

or

Register

labs