0

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.

hata
  • 11,633
  • 6
  • 46
  • 69
  • what exactly isn't working? you can try Modernizr or grab their code http://stackoverflow.com/questions/7867807/detecting-html5-audio-support-with-modernizr – llamerr Aug 13 '15 at 13:29
  • i am using this with bot and bot does not have audio support i want if this a bot get file 2 and if its real human get file 1 – abod alabhawi Aug 13 '15 at 13:31
  • what bot? is that some headless browser or what? this doesn't answer the question of what exactly isn't working in your code – llamerr Aug 13 '15 at 13:32
  • first of all you trying to compare Object to true in `media == true`. i'm not sure why you do this... that said - better use Modernizr and their example – llamerr Aug 13 '15 at 13:34
  • some one programed bot to my site and the bot does not have audio support i want the audio code if audio not working open file1.php if audio not working or supported in browser open file2.php – abod alabhawi Aug 13 '15 at 13:37

1 Answers1

2

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