在浏览器中设置视频自动播放的Cookie [英] Setting Cookies in browser for video autoplay

查看:748
本文介绍了在浏览器中设置视频自动播放的Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置Cookie,以便视频仅在首次访问时自动播放,之后如果他们想观看,则必须手动播放。

解决方案

这是我在项目上使用的:

  if(document.cookie.length = = 0 || document.cookie.indexOf(MYCOOKIENAME =)== -1){
//我将路径设置为/所以一旦他们在网站上看过一次,他们不会
//在其他页面上看到它。
document.cookie =MYCOOKIENAME = true; path = /;;

//开始播放视频。
}



我真的不想要添加cookie库的开销。 p>

将我的代码插入到Oliver Moran的HTML中即可:

 < iframe title =YouTube video playerid =videoframewidth =480height =390src =frameborder =0allowfullscreen>< / iframe> 

< script language =javascript>
var link =http://www.youtube.com/embed/he5fpsmH_2g;

if(document.cookie.length == 0 || document.cookie.indexOf(MYCOOKIENAME =)== -1){
//我将路径设置为/ so一旦他们在网站上看到它,他们不会
//在其他页面看到它。
document.cookie =MYCOOKIENAME = true; path = /;;

link + =?autoplay = 1; //将自动播放标签附加到视频网址
}

document.getElementById(videoframe)。src = link; //设置iframe src

< / script>


How would I set cookies such that a video only plays automatically on the first visit only, afterwards if they want to watch it, it must be played manually?

解决方案

Here's what I used on a project:

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

I didn't really want the overhead of adding a cookie library.

Plugging my code into Oliver Moran's HTML gives you:

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

这篇关于在浏览器中设置视频自动播放的Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆