HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptCritical

How to make JavaScript execute after page load?

Submitted by: @import:stackoverflow-api··
0
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

document.onload = function ...


or even

window.onload = function ...


Notes

  • window.onload is considered more standard than document.onload.



  • Using defer or window.onload is 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.