Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hook the DOM loaded event?

Tags:

html

dom

load

Can someone point me in the direction to hook the DOM loaded event?

Basically, i want to display a loader while the dom is loading (I dont mean Ajax requests- the first time a user hits a page) ?

Thanks all in advance

like image 582
Orry Avatar asked Feb 24 '09 16:02

Orry


2 Answers

If you're not using a framework, use the DOMContentLoaded event:

document.addEventListener('DOMContentLoaded', function() {
  // ...
})
like image 88
Elliot Winkler Avatar answered Jan 02 '23 07:01

Elliot Winkler


All of the popular Javascript libraries have a "DOM loaded" event you can use for this.

Essentially:

<html>
<head>
    <script>
    // if using jQuery
    $(document).ready(function() { $('#loading').hide(); });
    // if using Prototype
    document.observe("dom:loaded", function() { $('loading').hide(); });
    </script>
</head>
<body>
    <div id="loading">Loading...</div>
    <!-- rest of page -->
</body>
</html>
like image 31
Paolo Bergantino Avatar answered Jan 02 '23 07:01

Paolo Bergantino