0

I thought I had a pretty simple input onclick and script to play a random audio file.

Everything works locally on my system without a problem. Online the code is working on Chrome, Firefox and IE but not Safari. As it works locally on Safari, I'm struggling to find the issue and wondered if anyone could point me in the right direction? There are no error's logged in Safari's Web Inspector either!

<input class="btn btn-primary showPointer" type="button" onclick="getRandomSounds()" value="Play A Clip" />

<script type="text/javascript">
  var sounds = new Array();
  sounds[0] = "example.mp3";
  sounds[1] = "example1.mp3";

  function getRandomSounds() {
    var randomNum = Math.floor(Math.random()*sounds.length);
    document.getElementById("ashboxplayer").src=sounds[randomNum];
  }
</script>

The array has been shortened somewhat for this example but there are 80 objects in the array.

Thanks in advance for any light anybody can shed!

Cheers

Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303
AGW
  • 1
  • 1
    Please define "_doesn't work_". – Teemu Oct 03 '17 at 07:34
  • try using onfocus instead, might work, didnt test – Dellirium Oct 03 '17 at 07:44
  • Tried onfocus, no luck... – AGW Oct 03 '17 at 08:00
  • @Teemu nothing happens after clicking the button in Safari! (It's fine in Chrome, FF, IE and works fine on Safari locally). You can see for yourself here: www.ashgould.com (it's the 'ashbox') – AGW Oct 03 '17 at 08:02
  • What doesn't work? What if you change the function to `console.log` or `alert` something? Is the problem with playing the sound or is it really with the click event? What happens if you set the `src` of the ` – Quentin Oct 03 '17 at 08:58

2 Answers2

0

Figured it out.

Safari have content blockers and auto-play blockers. Once disabled for the website, everything is functioning correctly.

Thanks everyone for their assistance!

AGW
  • 1
0

try this

    <input class="btn btn-primary showPointer" type="button" id="randombtn"  value="Play A Clip" />
     var sounds = new Array();
      sounds[0] = "example.mp3";
      sounds[1] = "example1.mp3";
    $('#randombtn).on('click',function(){
   var randomNum = Math.floor(Math.random()*sounds.length);
        document.getElementById("ashboxplayer").src=sounds[randomNum];

    });
nikita
  • 324
  • 1
  • 7