我如何设置Cookie,以便视频仅在首次访问时自动播放,之后如果他们想要观看,则必须手动播放?
答案 0 :(得分:1)
一般的想法是:
页面加载检索cookie信息
如果没有cookie或设置为false,则播放电影
将cookie设置为true
答案 1 :(得分:1)
这是我在项目中使用的内容:
if (document.cookie.length == 0 || document.cookie.indexOf("MYCOOKIENAME=") == -1) {
// I set the path to / so once they'd seen it once on the site they wouldn't
// see it on other pages.
document.cookie = "MYCOOKIENAME=true; path=/;";
// START VIDEO PLAYING HERE.
}
我真的不想要添加cookie库的开销。
将我的代码插入Oliver Moran的HTML中会给你:
<iframe title="YouTube video player" id="videoframe" width="480" height="390" src="" frameborder="0" allowfullscreen></iframe>
<script language="javascript">
var link = "http://www.youtube.com/embed/he5fpsmH_2g";
if (document.cookie.length == 0 || document.cookie.indexOf("MYCOOKIENAME=") == -1) {
// I set the path to / so once they'd seen it once on the site they wouldn't
// see it on other pages.
document.cookie = "MYCOOKIENAME=true; path=/;";
link += "?autoplay=1"; // append an autoplay tag to the video URL
}
document.getElementById("videoframe").src = link; // set the iframe src
</script>
答案 2 :(得分:0)
根据Justin808的回答,一般的想法是这样的:
if (!cookieIsSet()) {
setCookie();
playMovie();
}
请参阅W3Schools网站,了解类似于您希望实现的Cookie的示例:http://www.w3schools.com/js/js_cookies.asp
如果您要嵌入YouTube视频,可以这样做:
<iframe title="YouTube video player" id="videoframe" width="480" height="390" src="" frameborder="0" allowfullscreen></iframe>
<script language="javascript">
var link = "http://www.youtube.com/embed/he5fpsmH_2g";
if (!cookieIsSet()) {
setCookie();
link += "?autoplay=1"; // append an autoplay tag to the video URL
}
document.getElementById("videoframe").src = link; // set the iframe src
</script>
显然,您必须定义自己的cookieIsSet()
和setCookie()
函数。有关如何使用的示例,请参阅W3School的网站。