在浏览器中设置 Cookie 以实现视频自动播放

发布于 2024-10-27 19:26:29 字数 54 浏览 3 评论 0原文

我如何设置 cookie,使视频仅在第一次访问时自动播放,之后如果他们想观看,则必须手动播放?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笨死的猪 2024-11-03 19:26:29

总体思路是:

  1. 在页面加载时检索 cookie 信息

  2. 如果没有 cookie,或其设置为 false,则播放电影

  3. 将 cookie 设置为 true

The general idea would be:

  1. on page load retrieve cookie information

  2. if no cookie, or its set to false, play the movie

  3. set cookie to true

薆情海 2024-11-03 19:26:29

这是我在项目中使用的:

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>

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>
我一直都在从未离去 2024-11-03 19:26:29

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

显然,您必须定义自己的 < code>cookieIsSet() 和 setCookie() 函数。有关示例,请参阅 W3School 网站。

Per Justin808's answer, the general idea would be like this:

if (!cookieIsSet()) {
  setCookie();
  playMovie();
}

See the W3Schools site for an example use of cookies similar to what you want to achieve: http://www.w3schools.com/js/js_cookies.asp

If you are embedding a YouTube video you could do it like this:

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

Obviously, you would have to define your own cookieIsSet() and setCookie() functions. See the W3School's site for examples how.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文