lundi 11 mai 2015

add attribure to html tag with javascript

I am using a javascript code to detect if a video is loaded.

Once it is loaded I want to add an autoplay attribute to the tag to make it play but I can't find a way to add that attribute. here is the code I use:

window.addEventListener('load', function() {
    var video = document.querySelector('#bgvid');
    var div = document.getElementById('#bgvid');

    function checkLoad() {
        if (video.readyState === 4) {
            alert('video is loaded')
            video.setAttribute("autoplay")
        } else {
            setTimeout(checkLoad, 100);
        }
    }

    checkLoad();
}, false);

******************* THE SOLUTION ********************

First, thanks DontVoteMeDown for the help.

proper code should be:

document.getElementById('bgvid').addEventListener('canplaythrough', function() {
    this.play();
    });

Aucun commentaire:

Enregistrer un commentaire