Cookie每次访问时仅自动播放<video>一次

时间:2016-07-21 02:13:50

标签: javascript video cookies autoplay

当用户登陆主页时,我想要自动播放视频。如果用户点击回家,我不希望视频自动播放。

我已使用以下脚本进行了设置,但这不起作用。

<script type="text/javascript" language="Javascript">// 
    function played(){
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf("played=") == 0) return 1;
    }
    var date = new Date();
    var days = 7;
    date.setTime(date.getTime()+(days*24*60*60*1000));
    document.cookie = "played=1"+"; expires="+date.toGMTString()+"; path=/";
    return 0;
    }
        if(played()==0){
        document.write("<video controls poster=\"http://acceptdifference.org.au/wp-content/uploads/2016/07/Accept-difference-fallback.jpg\" class=\"fillWidth\"><source src=\"https://s3-ap-southeast-2.amazonaws.com/acceptdifference/30sec_14_07_Youtube_ClosedCaptions.mp4.mp4\" /><source src=\"https://s3-ap-southeast-2.amazonaws.com/acceptdifference/30sec_14_07_Youtube_ClosedCaptions.webmhd.webm\" /><source src=\"https://s3-ap-southeast-2.amazonaws.com/acceptdifference/30sec_14_07_Youtube_ClosedCaptions.oggtheora.ogv\" /><img src=\"http://acceptdifference.org.au/wp-content/uploads/2016/07/Accept-difference-fallback.jpg\" title=\"Your browser does not support the video tag\"></video>");
    }
    else
    {
        document.write("<video controls autoplay poster=\"http://acceptdifference.org.au/wp-content/uploads/2016/07/Accept-difference-fallback.jpg\" class=\"fillWidth\"><source src=\"https://s3-ap-southeast-2.amazonaws.com/acceptdifference/30sec_14_07_Youtube_ClosedCaptions.mp4.mp4\" /><source src=\"https://s3-ap-southeast-2.amazonaws.com/acceptdifference/30sec_14_07_Youtube_ClosedCaptions.webmhd.webm\" /><source src=\"https://s3-ap-southeast-2.amazonaws.com/acceptdifference/30sec_14_07_Youtube_ClosedCaptions.oggtheora.ogv\" /><img src=\"http://acceptdifference.org.au/wp-content/uploads/2016/07/Accept-difference-fallback.jpg\" title=\"Your browser does not support the video tag\"></video>");
    }
</script>

1 个答案:

答案 0 :(得分:0)

function played(){
for(){
return 1; //does not work
}
return 0;
}

创建变量并将其返回

function played(){
a=0;
for(){
a=1; //does work
}
return a;
}