Issue
I want to detect when the <body>
element finishes loading into the DOM from an external script without using an external JavaScript library. I do not want to use document.ready
or window.onload
because they do not fire until the entire DOM (including all external files) finishes loading.
Solution
Here's a simple solution that works in ALL browsers:
Insert the following code above the
</body>
tag in the HTML:<script type="text/javascript"> try { ONLOAD() } catch(e) { ONLOADq=1 } </script> MINIFIED: <script type="text/javascript">try{ONLOAD()}catch(e){ONLOADq=1}</script>
Insert this code in the external javascript:
if ( typeof ONLOADq === 'number') { ONLOAD() };
Place all the code in the external javscript that needs to be run on the page's load into a function called
ONLOAD
. For example:function ONLOAD() { document.body.appendChild(ST1) };
The first part covers instances when the external javascript finishes loading before the HTML, and the second part, vice-versa.
Answered By - Brandon Lebedev Answer Checked By - Mildred Charles (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.