Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Ask HN: What is the best way to start autoplay?
1 point by maxraz on Nov 4, 2018 | hide | past | favorite | 2 comments
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.



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.


Thank you, Sir.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: