TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

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

1 pointsby maxrazover 6 years ago
As you know, some browsers prevent &#x27;autoplay&#x27; in &lt;audio id=&quot;sound&quot; src=&quot;track.mp3&quot; autoplay&gt;&lt;&#x2F;audio&gt;. I have 2 solutions and need to choose only one:<p>1) &lt;body onclick=&quot;document.getElementById(&#x27;sound&#x27;).play();&quot;&gt;<p>VS<p>2) document.addEventListener(&#x27;click&#x27;, musicPlay); function musicPlay() { document.getElementById(&#x27;sound&#x27;).play(); }<p>Thank you.

1 comment

mtmailover 6 years ago
Both do the same. (2) is more modern syntax as onclick DOM attributes are deprecated.<p><a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;</a> might be better suited for specific programming language questions.
评论 #18376862 未加载