document.onDO...">

Javascript onDOMContentLoaded not starting

Any ideas why this code is not working?

<html><head>
    <script type="text/javascript">
        document.onDOMContentLoaded=function(){
            alert('aaaaaaaaaaaaaa');
        }
    </script>
    </head>
    <body>
        <div id="mydiv"></div>
    </body>
</html>

onDOMContentLoaded is expected to trigger when the webpage loads and this warning appears, but it doesn’t work for a reason, why

+5
source share
1 answer

You must bind to an event with addEventListener:

document.addEventListener("DOMContentLoaded", function() {
    alert('aaaaaaaaaaaaaa');
});

http://jsfiddle.net/qHa4T/1

Keep in mind that both addEventListenerand DOMContentLoadedwill not work with IE8 and below.

+9
source

All Articles