fire off some javascript on page load 1

fire off some javascript on page load

document.addEventListener("DOMContentLoaded", theDomHasLoaded, false);
window.addEventListener("load", pageFullyLoaded, false);

function theDomHasLoaded(e) {
    // do something
}

function pageFullyLoaded(e) {
    // do something again
}

Here is what the above code is Doing:
1. The first line of code is a comment. It’s not necessary, but it’s good practice to include comments in your code.
2. The second line of code is a function declaration. It’s a function named theDomHasLoaded.
3. The third line of code is a function declaration. It’s a function named pageFullyLoaded.
4. The fourth line of code is an event listener. It’s listening for the DOMContentLoaded event. When the DOMContentLoaded event fires, the theDomHasLoaded function will be called.
5. The fifth line of code is an event listener. It’s listening for the load event. When the load event fires, the pageFullyLoaded function will be called.
6. The sixth line of code is the theDomHasLoaded function. It’s empty, but you can add code to it.
7. The seventh line of code is the pageFullyLoaded function. It’s empty, but you can add code to it.

Similar Posts