Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript onDOMContentLoaded doesn't trigger

Any ideas why this code doesn't work?

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

onDOMContentLoaded is expected to triogger when the webpage is loaded and make that alert but it doesn't work dunno why

like image 273
ionescho Avatar asked Jul 20 '12 16:07

ionescho


1 Answers

You should be binding to the event with addEventListener:

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

http://jsfiddle.net/qHa4T/1

Keep in mind that both addEventListener and DOMContentLoaded won't work with IE8 and below.

like image 168
bfavaretto Avatar answered Sep 28 '22 12:09

bfavaretto