How to run a function when the page is loaded?

前端 未结 9 1627
小鲜肉
小鲜肉 2020-11-22 11:02

I want to run a function when the page is loaded, but I don’t want to use it in the tag.

I have a script that runs if I initialise it in th

9条回答
  •  不知归路
    2020-11-22 11:58

    Try readystatechange

    document.addEventListener('readystatechange', () => {    
      if (document.readyState == 'complete') codeAddress();
    });
    

    where states are:

    • loading - the document is loading (no fired in snippet)
    • interactive - the document is parsed, fired before DOMContentLoaded
    • complete - the document and resources are loaded, fired before window.onload

    
    
    

提交回复
热议问题