How to check if DOM is ready without a framework?

前端 未结 6 1263
有刺的猬
有刺的猬 2020-11-27 12:39

The question is so like a zillion others here and on the web - How to check if DOM has loaded in Javascript? But here\'s the catch:

  • Without using a framework l
6条回答
  •  天涯浪人
    2020-11-27 12:57

    This works for all browsers and is short and concise:

    var execute = function () {
      alert("executing code");  
    };
    
    if ( !!(window.addEventListener) )
      window.addEventListener("DOMContentLoaded", execute)
    else // MSIE
      window.attachEvent("onload", execute)
    

提交回复
热议问题