How to Autoplay a Video Element Using JavaScript

Follow me!

If you like this article, go ahead and follow me on Twitter:

Follow @getButterfly

The latest Chrome version has stopped video autoplay if the video is not muted. Firefox and the other browsers will soon follow suit. Here’s a quick way to mute the video and autoplay it when the page has loaded:

function toggleMute(element) {
    element.muted = true;
    element.play()
}

window.addEventListener('load', function () {
    setTimeout(function () {
        toggleMute(document.querySelector('video'));
    }, 1000);
});

That’s it! Enjoy!

Find more JavaScript tutorials, code snippets and samples here or more jQuery tutorials, code snippets and samples here.

Find more JavaScript tutorials, code snippets and samples here or more jQuery tutorials, code snippets and samples here.

👋 Added by Ciprian on Friday, September 28, 2018 in Blog, JavaScript. Last modified on Saturday, September 29, 2018.

Contribute!

Contribute to this article by sharing your opinion on Twitter:

Unlimited Automated Page Speed Monitoring & Tracking.
Use SpeedFactor to track your website. It’s simple and reliable.
See how real people experience the speed of your website. Then find (and fix) your web performance problems.
Get Started
Privacy Policy