How to make JavaScript execute after page load?

前端 未结 24 2395
孤城傲影
孤城傲影 2020-11-21 05:55

I\'m executing an external script, using a

24条回答
  •  盖世英雄少女心
    2020-11-21 06:08

    I find sometimes on more complex pages that not all the elements have loaded by the time window.onload is fired. If that's the case, add setTimeout before your function to delay is a moment. It's not elegant but it's a simple hack that renders well.

    window.onload = function(){ doSomethingCool(); };
    

    becomes...

    window.onload = function(){ setTimeout( function(){ doSomethingCool(); }, 1000); };
    

提交回复
热议问题