根据cookie添加叠加层

时间:2012-04-27 05:30:02

标签: javascript jquery

我正在添加以下代码以添加叠加层

$(function () {

    {
        $('#overlay').fadeIn('fast', function () {
            $('#overlaybox').animate({ 'top': '90px' }, 500);
            //$('#box').css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
            $('#overlaybox').css("left", (($(window).width() - $('#overlaybox').outerWidth()) / 2) + $(window).scrollLeft() + "px");

            $('#nabshow').fadeOut('fast');

          })
    }



});

我发现以下代码根据cookie的可用性打开弹出窗口:

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

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);
    }
  }
}

function showPopupOnce() {
   var hasSeenPopup = myGetCookie("has_seen_popup");
  if (hasSeenPopup === null || hasSeenPopup === ""){
     // the user has never seen the popup, so show him!
    window.open("http://mywebsite.com/popup.html", "myPopupWindow");
   }

   // either way, set the cookie so the user will never see the window again
   mySetCookie("has_seen_popup", "true", 365); // 365 days = 1 year
}

<body onLoad="showPopupOnce();">

我需要在叠加代码中进行哪些更改,以确保它只显示一次。 我将添加get cookie和setcookie函数,然后我该怎么做:

由于 ARNAB

1 个答案:

答案 0 :(得分:1)

你已经做好了一切!您只需编辑showPopupOnce()代码即可。

function showPopupOnce() {
    var hasSeenPopup = myGetCookie("has_seen_popup");
    if (hasSeenPopup === null || hasSeenPopup === "") {
        // the user has never seen the popup, so show him!
        $('#overlay').fadeIn('fast', function() {
            $('#overlaybox').animate({
                'top': '90px'
            }, 500);
            //$('#box').css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
            $('#overlaybox').css("left", (($(window).width() - $('#overlaybox').outerWidth()) / 2) + $(window).scrollLeft() + "px");
            $('#nabshow').fadeOut('fast');
        })
        window.open("http: //mywebsite.com/popup.html", "myPopupWindow");
        }
        // either way, set the cookie so the user will never see the window again
        mySetCookie("has_seen_popup", "true", 365); // 365 days = 1 year
    }
}​

现在您在匿名函数中有覆盖代码。因此,每次加载页面时都会运行。在没有cookie的情况下将代码移到showPopup函数中;它会做你想做的事。

如果您需要任何澄清/帮助,请与我们联系。