Skip to content

Instantly share code, notes, and snippets.

@mileskrell
Created March 6, 2020 08:16
Show Gist options
  • Save mileskrell/700c1207217e0f9626b405fbd5d7992a to your computer and use it in GitHub Desktop.
Save mileskrell/700c1207217e0f9626b405fbd5d7992a to your computer and use it in GitHub Desktop.
Try to detect if autoplay on videos is blocked
<video id="vid" autoplay loop muted>
<source src="/vid.mp4" type="video/mp4" />
</video>
<script>
setTimeout(function() {
let vid = document.getElementById("vid");
if (vid.paused) {
let message = document.createElement("p");
message.innerHTML = "There's supposed to be a video here, but your browser either " +
"doesn't support the video element or is blocking autoplay";
vid.replaceWith(message);
}
}, 100);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment