Native JS achieve dom ready

The record about the project technical issues:

Remember: on the script tag in the head, execution first time

var baseTools = {
  // dom ready
  ready: function( f ){
    var ie = !!(window.attachEvent && !window.opera);
    var wk = /webkit\/(\d+)/i.test(navigator.userAgent) && (RegExp.$1 < 525);
    var fn = [];
    var run = function () { for (var i = 0; i < fn.length; i++) fn[i](); };
    var d = document;

    if (!ie && !wk && d.addEventListener)
      return d.addEventListener('DOMContentLoaded', f, false);
    if (fn.push(f) > 1) return;
    if (ie)
      (function () {
        try { d.documentElement.doScroll('left'); run(); }
        catch (err) { setTimeout(arguments.callee, 0); }
      })();
    else if (wk)
      var t = setInterval(function () {
        if (/^(loaded|complete)$/.test(d.readyState))
          clearInterval(t), run();
      }, 0);
  }
};

These are the native JS implementation dom ready, followed by the use of:

; ( Function () { 
  baseTools.ready ( function () { 
    you want to achieve the code 
  }); 
}) ();

 

Guess you like

Origin www.cnblogs.com/XiaoYEBLog/p/11429789.html