Video autoplay Property - how to create a <video> element and setting the autoplay property: - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Video

Description

Video autoplay Property - how to create a <video> element and setting the autoplay property:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction(true)">Video with autoplay</button>
<button onclick="myFunction(false)">Video without autoplay</button>
<br>

<script>
function myFunction(p) {
    var x = document.createElement("VIDEO");
    x.setAttribute("id", "myVideo");
    x.setAttribute("controls", "controls");
    var y = document.createElement("SOURCE");
    y.setAttribute("src", "your.mp4");
    y.setAttribute("type", "video/mp4");
    x.appendChild(y);//from w w w . ja v a2  s. c  o m
    var z = document.createElement("SOURCE");
    z.setAttribute("src", "your.ogg");
    z.setAttribute("type", "video/ogg");
    x.appendChild(z);

    // Set the autoplay property:
    x.autoplay = p;

    document.body.appendChild(x);
}
</script>

</body>
</html>

Related Tutorials