Cookie自动播放<视频>每次访问仅一次

Cookie to autoplay <video> only once per visit

本文关键字:一次 访问 自动播放 lt 视频 gt Cookie      更新时间:2023-09-26

当用户登录主页时,我有一个视频要自动播放。如果用户点击返回主页,我不希望视频自动播放。

我已经用下面的脚本设置了这个,但这不起作用。

<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>
function played(){
for(){
return 1; //does not work
}
return 0;
}

创建一个变量并将其返回

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