How to run a function when the page is loaded?

前端 未结 9 1657
小鲜肉
小鲜肉 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 12:07

    Rather than using jQuery or window.onload, native JavaScript has adopted some great functions since the release of jQuery. All modern browsers now have their own DOM ready function without the use of a jQuery library.

    I'd recommend this if you use native Javascript.

    document.addEventListener('DOMContentLoaded', function() {
        alert("Ready!");
    }, false);
    

提交回复
热议问题