Ask HN: What is the best way to start autoplay?

1 points by maxraz ↗ HN
As you know, some browsers prevent 'autoplay' in <audio id="sound" src="track.mp3" autoplay></audio>. I have 2 solutions and need to choose only one:

1) <body onclick="document.getElementById('sound').play();">

VS

2) document.addEventListener('click', musicPlay); function musicPlay() { document.getElementById('sound').play(); }

Thank you.

2 comments

[ 7.3 ms ] story [ 30.0 ms ] thread
Both do the same. (2) is more modern syntax as onclick DOM attributes are deprecated.

https://stackoverflow.com/ might be better suited for specific programming language questions.