snippetjavascriptCritical
How to make JavaScript execute after page load?
Viewed 0 times
pagehowloadaftermakeexecutejavascript
Problem
I'm executing an external script, using a `
inside .
Now since the script executes before the page has loaded, I can't access the `, among other things. I'd like to execute some JavaScript after the document has been "loaded" (HTML fully downloaded and in-RAM). Are there any events that I can hook onto when my script executes, that will get triggered on page load?Solution
These solutions will work:
As mentioned in comments use defer:
or
or
or even
Notes
As mentioned in comments use defer:
or
or
document.onload = function ...or even
window.onload = function ...Notes
window.onloadis considered more standard thandocument.onload.
- Using
deferorwindow.onloadis unobstrusive.
Code Snippets
<script src="deferMe.js" defer></script><body onload="script();">document.onload = function ...window.onload = function ...Context
Stack Overflow Q#807878, score: 1228
Revisions (0)
No revisions yet.