用于在第x次页面浏览后显示内容的JS cookie

问题描述 投票:0回答:1

我试图找到如何设置一个javascript cookie,以便在访问者的第4次浏览后显示一个fancybox弹出窗口。

这是我用来显示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>

但它不起作用。

至于控制网页浏览量,我不知道如何设置,也无法找到任何资源来帮助我完成这项工作。

非常感谢

jquery cookies fancybox fancybox-2 pageviews
1个回答
4
投票

Update April 02, 2019

正如@Gregdebrick的评论中所提到的,答案是更新使用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

© www.soinside.com 2019 - 2024. All rights reserved.