Mich'
Mich'

Reputation: 697

JavaScript / HTML - new Audio JavaScript fails in IE 8 and less?

How can I fix this? eg:


snd = new Audio("foo/bar.wav")

alert("abc") //in IE this line wont run??!

snd.play()

Upvotes: 2

Views: 1733

Answers (2)

Mooseman
Mooseman

Reputation: 18891

IE8- does not support the <audio> element. See http://caniuse.com/#feat=audio. If you can separate your script, you can do this:

<!--[if lte IE 8]>
  <script>
    alert("Old IE.");
  </script>
<![endif]-->
<!--[if IE 9]>
  <script>
    snd = new Audio("foo/bar.wav");
    alert("IE9");
    snd.play()
  </script>
<![endif]-->
<!--[if !IE]> -->
  <script>
    snd = new Audio("foo/bar.wav");
    alert("Not IE (Or IE10+)");
    snd.play()
  </script>
<!-- <![endif]-->

IE10 drops conditional comments altogether, so it will use the !IE script.

Upvotes: 0

pce
pce

Reputation: 5931

If you detect the feauture (the supported type), your script will not throw an error in other browsers (mobile etc.), that doesn't support audio.

You could make it crossbrowser conditional ie.

if (typeof window.Audio != 'undefined') { 
  /* var snd = new Audio("foo/bar.wav"); ... */ 
} 

or consider to use Modernizr.

Upvotes: 2

Related Questions