News for hackers. Stuff that matters.

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

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 | on HN )
Re:Ask HN: What is the best way to start autoplay? by mtmail on Sunday November 04, 2018 @07:10AM (#18375501)
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.

Re:Ask HN: What is the best way to start autoplay? by maxraz on Sunday November 04, 2018 @12:17PM (#18376862)
Thank you, Sir.