Geek asks and answers

"DomContentLoaded" doesn't work in my browser?

I wrote my html page as follows:

<div>
    <img src="Natural1.jpg" id="img1" >
 <audio src="sound.mp3" id="audio1" ></audio>
</div>

And my javascript file is like this:

function init(){
    audio1 = document.getElementById("audio1");
    var img1 = document.getElementById("img1");
    img1.addEventListener("click", imgClick, false);
}
function imgClick(){
    if(audio1.paused){
        audio1.play();
    }
    else{
        audio1.pause();
    }
}
document.addEventListener('DomContentLoaded', init, false);

I ran this in chrome12, the script first executed the method document.addEventListener, but it did not get into the init method, why? I tried the method attachEventinstead addEventListenerin IE8, but it still doesn't do it. what is wrong with my code?

+3
javascript google-chrome addeventlistener
Jack zhang May 04 '12 at 3:02
source share
2 answers

It may be a wrapper for the name of an event. Instead, try the following:

document.addEventListener('DOMContentLoaded', init, false);

Pretty sure that is so. I tested the following:

document.addEventListener("DomContentLoaded", function(){ alert('Dom') }, false);
document.addEventListener("DOMContentLoaded", function(){ alert('DOM') }, false);

jsbin, . , IE8 , . IE9 IE10.

: http://jsbin.com/ocidok/edit#javascript,html

DOMContentLoaded Mozilla Developer Network. :

enter image description here

+7
Sampson 04 '12 3:05

JavaScript - . , . , 'DomContentLoaded' 'DomContentLoaded' - , Chrome.

IE 8: . IE, - DOMContentLoaded HTML5, 9, : ! , 16--out-of-date Chrome 12, ? , . . , , Chromebook, - - ...

0
strawn_04 14 . '13 16:35

More articles:

  • Play 2 scala route controller bytecode does not match all bytecode - scala
  • PHP to view PDF on a web page and disable user to download - php
  • Div does not cover the whole page? - html
  • HttpWebRequest scope response stream - c #
  • Проверка уникальности для accepts_nested_attributes_for модели - validation
  • How to unit test Java code that dynamically displays html and css? - java
  • The correct way is PHPunit test mock getRequest-> getHeader ('referer') - oop
  • how to clear graphics (rectangle shape) in image window - c #
  • It is impossible to use "what mysql" in zsh, but in bash it works - bash
  • setinterval () and clearinterval () - when cleared, not animated automatically - jquery

All Articles

Geek-Ask | 2020