• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 76
  • Last Modified:

Javascript global variable only recognizable using Microsoft browsers--not with other browsers

I am trying to figure out why the following code works with Microsoft browsers (IE / Edge) but not with any other web browser. Does anyone have any ideas?

<html>
<head>
<script type="text/javascript">
  var URL='http://www.noiseaddicts.com/samples_1w72b820/3718.mp3';
</script>
</head>
<body> 
<audio controls>
<source id="desktop1" src="" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<script type="text/javascript">
window.onload=function(){ 
  document.getElementById('desktop1').setAttribute('src', URL);
};
</script>
</body>
</html>

Open in new window

0
Monroe406
Asked:
Monroe406
1 Solution
 
Chris StanyonCommented:
Nothing wrong with your variables - you just need to call load() on the audio after you've set the src:

<audio controls id="myAudio">
...
window.onload=function(){ 
  document.getElementById('desktop1').setAttribute('src', URL);
  document.getElementById('myAudio').load();
};

Open in new window

0
 
Leonidas DosasCommented:
Use only audio element

<html>
<head>
<script type="text/javascript">
  var URL='https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3';
</script>
</head>
<body> 
<audio id="desktop1" controls="controls" src="" autoplay>
</audio>
<script type="text/javascript">
window.onload=function(){ 
  document.getElementById('desktop1').setAttribute('src', URL);
};
</script>
</body>
</html>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now