8

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

gen_Eric
228k42 gold badges304 silver badges343 bronze badges
asked Jul 20, 2012 at 16:07
1
  • 8
    +1 for summing up how I feel whenever programming in JavaScript: 'aaaaaaaaaaaaaa'!!! Commented Mar 23, 2014 at 6:40

1 Answer 1

16

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.

answered Jul 20, 2012 at 16:12
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.