To determine if the variable audioElement
is defined, you can simply check for its existence:
if (audioElement) { ... audioElement is present in the code ... }
When you declare this element in the <head>
section, it essentially becomes a global variable.
This snippet of code checks whether or not you have initialized the audioElement variable. To verify HTML5 audio support in the browser, you may want to evaluate the Modernizr.audio
attribute, like so:
if (Modernizr.audio) {
var audio = new Audio();
audio.src = Modernizr.audio.ogg ? 'song.ogg' :
Modernizr.audio.mp3 ? 'song.mp3' :
'song.m4a'
;
audio.play();
}
Check out this demo; be sure to replace 'song.mp3' with your own audio file link. )