Fancybox弹出一次会话时间

我有一个带有fancybox的弹出窗口,显示在加载页面上。

我需要一次显示弹出窗口,如果用户更改页面并返回页面,弹出窗口不会再次显示。

我读过可以使用cookie插件( https://github.com/carhartl/jquery-cookie ),但我不明白如何集成这段代码……

我在html / css中有一个简单的网站。

这是代码:

           function openFancybox() { setTimeout(function () { $('#yt').trigger('click'); }, 500); }; $(document).ready(function () { var visited = $.cookie('visited'); if (visited == 'yes') { return false; // second page load, cookie active } else { openFancybox(); // first page load, launch fancybox } $.cookie('visited', 'yes', { expires: 7 // the number of days cookie will be effective }); $("#yt").click(function() { $.fancybox({ 'padding' : 0, 'autoScale' : false, 'transitionIn' : 'none', 'transitionOut' : 'none', 'title' : this.title, 'width' : 680, 'height' : 495, 'href' : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'), 'type' : 'swf', 'swf' : { 'wmode' : 'transparent', 'allowfullscreen' : 'true' } }); return false; }); });       

对于浏览器一致性,您可能需要首次延迟fancybox加载执行,因此请尝试以下代码:

 function openFancybox() { // launches fancybox after half second when called setTimeout(function () { $('#yt').trigger('click'); }, 500); }; $(document).ready(function () { var visited = $.cookie('visited'); // create the cookie if (visited == 'yes') { return false; // second page load, cookie is active so do nothing } else { openFancybox(); // first page load, launch fancybox }; // assign cookie's value and expiration time $.cookie('visited', 'yes', { expires: 7 // the number of days the cookie will be effective }); // your normal fancybox script $("#yt").click(function () { $.fancybox({ // your fancybox API options }); return false; }); }); 

请参阅此JSFIDDLE中的代码

注意

  • 为了看到cookie工作,您可能需要使用jsfiddle的全屏模式http://jsfiddle.net/aVE9N/show/
  • 我建议你更新(至少)你的fancybox版本从v1.3.1到v1.3.4
  • 假设您正在正确加载页面中的jQuery cookie插件

使用jQuery cookie插件,您建议:

 $(document).ready(function() { if(typeof $.cookie('popupVideoPlayed') == 'undefined') { $.cookie('popupVideoPlayed', 'true'); // set a session cookie so it won't play again $('#yt').trigger('click'); } }); 

请记住删除内联体onload事件处理程序。