Reputation: 19
I have JavaScript code for audio. I want to redirect to a URL if the browser has audio support. Also if the browser does not support audio, redirect to another link.
I tried this:
<script>
var audio= document.createElement("audio")
var media= {audio : (audio.play) ? true : false}
if (media == true) {
window.location.replace("file1.php");
} else {
window.location.replace("file2.php");
}
</script>
It didn't work.
Can anyone help? Thanks.
Upvotes: 0
Views: 73
Reputation:
I tried this on a personal site, and using the console as well, and worked like a charm, give it a try and let me know how it goes.
You were close enough creating and audio tag.
var audioTest = document.createElement('audio');
if( !!(audioTest.canPlayType && audioTest.canPlayType('audio/mpeg;')) ) {
// redirect
} else {
// redirect
}
Common audio type values (JIC):
video/ogg
video/mp4
video/webm
audio/mpeg
audio/ogg
audio/mp4
Upvotes: 2