PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 15, 2022

[FIXED] How do I check when the HTML component of the DOM has loaded without using a JavaScript library?

 October 15, 2022     dom, dom-events, javascript     No comments   

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:

  1. 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>
    
  2. Insert this code in the external javascript:

    if ( typeof ONLOADq === 'number') {
      ONLOAD()
    };
    
  3. 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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing