How do I call a JavaScript function on page load?

后端 未结 8 1344
轮回少年
轮回少年 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:35

    Another way to do this is by using event listeners, here how you use them:

    document.addEventListener("DOMContentLoaded", function() {
      you_function(...);
    });
    

    Explanation:

    DOMContentLoaded It means when the DOM Objects of the document are fully loaded and seen by JavaScript, also this could have been "click", "focus"...

    function() Anonymous function, will be invoked when the event occurs.

提交回复
热议问题