How to Play An Audio On Page Load Using Html?

Viewed 29

I tried playing an audio when the webpage loads, I used The Following Html Code For It.

<audio src="Pvs.mp3" id="my_audio" loop="loop"></audio>
  <script type="text/javascript">
    window.onload=function(){
      document.getElementById("my_audio").play();
    }
  </script>
  </audio>

I used The Above Code Inside the body tag But it's Not Working.. Is There Any Other Workibg Way To Play an Audio File On Page Load?

1 Answers

Try this, just before </body> ending tag

<script>  
window.addEventListener('load', () => {
document.getElementById("my_audio").play();
});  
</script>
Related