Getting DOMException with playing video onload

Refresh

April 2019

Views

4 time

0

I want to play a video when the website is loaded. For that I am using the following code:

window.onload = function webisteLoaded() {
  var main_bg_video = document.getElementById("main_bg_video");
  main_bg_video.play();
}
<video id="main_bg_video" height="100%" width="100%" autoplay="autoplay">
        <source src="./media/main/main.mp4" type="video/mp4">
        Your browser does not support HTML5 video.
      </video>

Additional info: I import the script in the <head/> tag. Sometimes, not every time I get this error:

 Uncaught (in promise) DOMException  

The error has the following details:

webisteLoaded   @   main.js:3
load (async)        
(anonymous) @   main.js:1  

Why I get this and how to fix it?
~filip

0 answers