0

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

$(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 函数,然后我该怎么办:

谢谢阿纳布

4

1 回答 1

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 函数;它会做你想做的事。

如果您需要任何澄清/帮助,请告诉我。

于 2012-04-27T05:47:01.517 回答