2011-03-30 19 views

回答

1

的总体思路是:

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

  2. 如果没有Cookie,或者其设置为false,播放影片

  3. 设置cookie的真

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库的开销。

堵我的代码为奥利弗·莫兰的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> 
+0

我收到一封电子邮件,询问如何让cookie比会话持续更久。要做到这一点,你需要告诉浏览器cookie何时到期,如果没有指定日期,它会将cookie视为会话cookie,这意味着一旦关闭浏览器,cookie的值就会丢失。您可以附加类似“; expires = Thu,2060年1月1日05:00:00 GMT;”放到我们分配给document.cookie的字符串上,它应该保留很长一段时间。 – drewish 2011-11-29 01:47:05

0

每Justin808的回答,一般的想法是这样的:

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

请参考w3School网站为例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的网站,了解示例。

相关问题