How do I call a JavaScript function on page load?

后端 未结 8 1517
轮回少年
轮回少年 2020-11-22 15:18

Traditionally, to call a JavaScript function once the page has loaded, you\'d add an onload attribute to the body containing a bit of JavaScript (usually only c

8条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 15:46

    Your original question was unclear, assuming Kevin's edit/interpretation is correct then this first option doesn't apply

    The typical options is using the onload event:

    
    ....
    

    You can also place your javascript at the very end of the body; it won't start executing until the doc is complete.

    
      ...
      
    
    

    And, another options, is to consider using a JS framework which intrinsically does this:

    // jQuery
    $(document).ready( function () {
      SomeFunction();
    });
    

提交回复
热议问题