Link to home
Start Free TrialLog in
Avatar of Monroe406
Monroe406

asked on

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

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

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

ASKER CERTIFIED SOLUTION
Avatar of Leonidas Dosas
Leonidas Dosas
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial