【问题标题】:JS cookie for displaying content after x-th pageview用于在第 x 次浏览后显示内容的 JS cookie
【发布时间】:2013-01-23 12:11:50
【问题描述】:

我正在尝试查找如何设置 javascript cookie,以便在访问者的第 4 次网页浏览之后显示一个花式框弹出窗口。

这是我用来显示 Fancybox 弹出窗口的代码,但正如您所见,这仅在第一次网页浏览时显示该代码,并且它会在一天后过期

function setCookie(c_name,value,exdays)
 {
     var exdate=new Date();
     exdate.setDate(exdate.getDate() + exdays);
     var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
     document.cookie=c_name + "=" + c_value + ";domain=.mysite.net;path=/";
 }

function getCookie(c_name)
 {
     var i,x,y,ARRcookies=document.cookie.split(";");
     for (i=0;i<ARRcookies.length;i++)
  {
     x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
     y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
     x=x.replace(/^\s+|\s+$/g,"");
      if (x==c_name)
       {
       return unescape(y);
       }
  }
 }

jQuery(document).ready(function() {
     var show = getCookie("fancyreg");
     if(show==null || show=="") {
       $.fancybox(
 {
        'type' : 'iframe',
        'href' : 'http://www.mysite.net/popup/', //URL to popup page or image
        'autoDimensions' : false,
        'width' : 480,
        'height' : 260,
        'transitionIn' : 'none',
        'transitionOut' : 'none'
 }
);

  setCookie('fancyreg','1',1); 

 }
 });

我还想请您帮助我如何在现有代码中添加延迟,以便在 3 秒(3000 毫秒)后显示弹出窗口。

我尝试使用 setTimeout(function() 如下

    <script type="text/javascript"> 
jQuery(document).ready(function() {
    setTimeout(function() {
        $.fancybox({
            'type' : 'iframe',
        'href' : 'http://www.mysite.net/popup/', //URL to popup page or image
        'autoDimensions' : false,
        'width' : 480,
        'height' : 260,
        'transitionIn' : 'none',
        'transitionOut' : 'none'
        })
    }, 4000); 
});
</script>

但它不起作用。

至于控制浏览量,我不知道如何设置,也找不到任何资源来帮助我解决这个问题。

非常感谢

【问题讨论】:

  • 嗨 Jan,我已经使用 setTimeout JS 函数添加了弹出延迟的代码。
  • 您可能需要使用 jQuery cookie 插件。查看stackoverflow.com/a/8305703/1055987 了解更多信息。您可以将 cookie 设置为 4 天后过期,然后打开 fancybox
  • 感谢肯尼迪。在第 3 次、第 4 次或第 x 次网页浏览后显示 fancybox 的任何提示。
  • @valdroni : 看看我的回答......如果你考虑的话,请做meta.stackexchange.com/a/5235

标签: jquery cookies fancybox fancybox-2 pageviews


【解决方案1】:

2019 年 4 月 2 日更新

正如@Gregdebrick 在 cmets 中提到的,答案正在更新为使用 JavaScript Cookie 插件 https://github.com/js-cookie/js-cookie 而不是已弃用的 jQuery Cookie 插件。

所以,在您的页面中从 CDN 加载 JS Cookie 插件后:

<script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>

使用以下脚本:

$(document).ready(function () {
    // if the cookie is undefined, create it
    if(typeof Cookies.get('visited') === "undefined"){
        Cookies.set("visited", 0);
    }
    // Cookies.get('visited') returns a string
    if (parseInt(Cookies.get('visited')) >= 3) {
        // open fancybox after 3 secs on 4th visit
        setTimeout(function () {
            $.fancybox.open({
                // your fancybox API options here
            });
        }, 3000);
    } else {
        let increase = parseInt(Cookies.get('visited')) + 1;
        Cookies.set('visited', increase, { expires: 1 });
        return false;
    }
}); // ready

updated working DEMO


假设您使用的是jQuery Cookie Plugin,那么您可以使用以下代码在3秒后启动fancybox,当天访问第4页:

$(document).ready(function () {
    // create cookie
    var visited = $.cookie('visited'); // visited = 0
    if (visited >= 3) {
        // open fancybox after 3 secs on 4th visit or further on the same day
        setTimeout(function () {
            $.fancybox.open({
                // your fancybox API options here
            });
        }, 3000);
    } else {
        visited++; // increase counter of visits
        // set new cookie value to match visits
        $.cookie('visited', visited, {
            expires: 1 // expires after one day
        });
        return false;
    }
}); // ready

working DEMO

【讨论】:

  • 嗨 @JFK,jQuery 插件现已弃用,您能否使用新插件 (github.com/js-cookie/js-cookie) 更新您的答案?你做了我正在寻找的东西,但是没有 JS 技能,我更新它很复杂。非常感谢
猜你喜欢
  • 2019-08-23
  • 2012-10-15
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 2020-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多