0

I'm trying to set up a client's website to play back an audio file on page load (at his demand, not a preferred feature for me). I have used some simple JavaScript but the audio isn't playing when the page loads. Any ideas why this is happening?

<head>
<!--other metadata up here-->
    <script>
function EvalSound(soundobj) {
      var thissound = document.getElementById(soundobj);
      thissound.Play();
    }
    </script>

    <embed src="scripts/audio.wav" autostart=true width=1 height=1 id="audio"
    enablejavascript="true">
</head>
<body>
  <div id="container" onload="EvalSound('audio')">
<!--rest of page>
</body>

2 Answers 2

1

HTML5 solution, (IE9+, and all other proper browsers are supported)

<audio src="audio.mp3" preload="auto" controls></audio>

<script>
    var getAudio = document.getElementsByTagName("audio")[0];
    getAudio.play();
</script>

Auto playing audio with no way to stop it is a horrible idea though.

Sign up to request clarification or add additional context in comments.

2 Comments

What's the best place to drop this within the HTML?
Put the audio tag in the body somewhere, and then the Javascript goes at the bottom, near the closing body tag.
0

Divs don't have a load event. You should probably attach it to the body.

1 Comment

I've tried doing this and also adding an empty form using the onload event for that to no avail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.