开发者

javascript callback works with IE, but not Chrome or Firefox

I think this code should play a sound, then show an alert box. It works with IE9, but with Chrome & Firefox, the sound is played, but the callback is never called - so no alert box.

I'm a c#, c++ programmer new to javascript, and I could use some help! Thanks.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <script type="text/javascript">
        var playAll= function(){
             var audio = document.getElementById('s');

             var callback = function(e){
                alert("ended"); 
             };

            audio.onended = callback;
            audio.play();
        }

    </script>
</head>

<body>
    <input name="But开发者_C百科ton1" type="button" value="Play" onclick="playAll()"/>

    <audio ID="s">
    <source src="s.mp3"  >
    <source src="s.ogg"  >
</body>

</html>


Just to be sure that it is not an issue of invalid html, use the following valid version

<audio ID="s">
  <source src="s.mp3" />
  <source src="s.ogg" />
</audio>

and then use the correct way to bind events.

audio.addEventListener( "ended", callback, false);


onended seems to be specific to IE9, Chrome and Firefox will fire the event ended, which you can bind a function to like so: v.addEventListener("ended", function() { alert("ended"); }

Remember that the HTML5 spec is still being developed, and not all browsers implement it the same way.

There's a similar question here: HTML 5 Video OnEnded Event not Firing

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜