【问题标题】:Cookie to autoplay <video> only once per visit每次访问仅自动播放一次 <video> 的 Cookie
【发布时间】:2016-07-21 02:13:50
【问题描述】:

我想在用户登陆主页时自动播放一个视频。如果用户点击返回首页,我不希望视频自动播放。

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

<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>

【问题讨论】:

  • 我建议您在每次请求时设置视频,使用本地存储而不是 cookie 来跟踪是否已播放,如果尚未播放,只需调用 myVideo.play()。简单得多。

标签: javascript video cookies autoplay


【解决方案1】:
function played(){
for(){
return 1; //does not work
}
return 0;
}

创建一个变量并返回它

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

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 2021-12-15
    • 2018-04-09
    • 2016-06-12
    • 1970-01-01
    相关资源
    最近更新 更多