I need to fire a script as soon as the page content (the whole HTML element) has been received, but it doesn't have to be rendered yet.
I assume that just having a simple tag that executes some code at the very top of my page should do the trick?
It's likely then that you want the DOMContentLoaded
event. You can use it thusly:
<head>
<!-- … -->
<script>
window.addEventListener('DOMContentLoaded',function () {
//your code here
});
</script>
</head>
To formulate the question differently: does DOM ready mean that all elements and resources have been pulled and rendered?
There is no "DOM ready" event. You're probably thinking of jQuery's .ready()
, or some similar odd use of this terminology (e.g. Google also has a similarly named proprietary event they refer to).
For the DOMContentLoaded
event, however (to quote MDN):
The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.