使用cookie隐藏弹出窗口

时间:2013-11-18 20:01:46

标签: javascript popup

以下代码在页面加载(body onload)时打开一个弹出窗口。当用户关闭弹出窗口时,我不希望它再次出现。据我所知,这可以通过cookie完成,但如何将其添加到此示例中?

<html>
<head>
<title>Popup</title>

<style type="text/css">
#blanket {
    display:none;
   background-color:#111;
   opacity: 0.65;
   *background:none;
   position:absolute;
   z-index: 9001;
   top:0px;
   left:0px;
   width:100%;
}

#popUpDiv {
    display:none;
    position:fixed;
    width:600px;
    height:400px;
    border:5px solid #000;
    z-index: 9002;
    padding: 16px;
    border: 5px solid #50563C;
    border-radius:15px;
    background-color: #FFFFFF;
}

</style>

<script type="text/javascript">
function toggle(div_id) {
    var el = document.getElementById(div_id);
    if ( el.style.display == 'none' ) { el.style.display = 'block';}
    else {el.style.display = 'none';}
}
function blanket_size(popUpDivVar) {
    if (typeof window.innerWidth != 'undefined') {
        viewportheight = window.innerHeight;
    } else {
        viewportheight = document.documentElement.clientHeight;
    }
    if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
        blanket_height = viewportheight;
    } else {
        if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
            blanket_height = document.body.parentNode.clientHeight;
        } else {
            blanket_height = document.body.parentNode.scrollHeight;
        }
    }
    var blanket = document.getElementById('blanket');
    blanket.style.height = blanket_height + 'px';
    var popUpDiv = document.getElementById(popUpDivVar);
    popUpDiv_height=blanket_height/2-200;//200 is half popups height


}
function window_pos(popUpDivVar) {
    if (typeof window.innerWidth != 'undefined') {
        viewportwidth = window.innerHeight;
    } else {
        viewportwidth = document.documentElement.clientHeight;
    }
    if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
        window_width = viewportwidth;
    } else {
        if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
            window_width = document.body.parentNode.clientWidth;
        } else {
            window_width = document.body.parentNode.scrollWidth;
        }
    }
    var popUpDiv = document.getElementById(popUpDivVar);
    window_width=window_width/2-300;//300 is half popups width
    popUpDiv.style.left = window_width + 'px';
}
function popup(windowname) {
    blanket_size(windowname);
    window_pos(windowname);
    toggle('blanket');
    toggle(windowname);     
}

</script>
</head>

<body onload="popup('popUpDiv')">

    <div id="blanket" style="display:none;"></div>
    <div id="popUpDiv" style="display:none;">

        <a href="#" onclick="popup('popUpDiv')" >Close</a>
    </div>  
  <a href="#" onclick="popup('popUpDiv')">Click to Open pop-up</a>

</body>
</html>

实际上,我有一段可行的代码,但不能与上面的JS和弹出窗口一起使用。

<script type="text/javascript">
       function setCookie(name, value, expires, path, domain, secure) {
           document.cookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
       }
       function getCookie(name) {
           var cookie = " " + document.cookie;
           var search = " " + name + "=";
           var setStr = null;
           var offset = 0;
           var end = 0;
           if (cookie.length > 0) {
               offset = cookie.indexOf(search);
               if (offset != -1) {
                   offset += search.length;
                   end = cookie.indexOf(";", offset);
                   if (end == -1) {
                       end = cookie.length;
                   }
                   setStr = unescape(cookie.substring(offset, end));
               }
           }
           if (setStr == 'false') {
               setStr = false;
           }
           if (setStr == 'true') {
               setStr = true;
           }
           if (setStr == 'null') {
               setStr = null;
           }
           return (setStr);
       }
       function hidePopup() {
           setCookie('popup_state', false);
           document.getElementById('popUpDiv').style.display = 'none';     document.getElementById('blanket').style.display = 'none';
       }
       function showPopup() {
           setCookie('popup_state', null);
           document.getElementById('popUpDiv').style.display = 'block'; document.getElementById('blanket').style.display = 'block';
       }
       function checkPopup() {
           if (getCookie('popup_state') == null) { // if popup was not closed
               document.getElementById('popUpDiv').style.display = 'block'; document.getElementById('blanket').style.display = 'block';
           }
       }

</script>

我尝试执行这两个脚本:

<body onload="popup('popUpDiv');'checkPopup()';">

我试图合并脚本,但我被卡住了。

1 个答案:

答案 0 :(得分:0)

也许试试这个:

function popup(windowname) {
    if ($.cookie('the_popup') !== '1') {
        $.cookie('the_popup', '1', {
          expires: 365,
          path: '/'
        });

        //Your code
        blanket_size(windowname);
        window_pos(windowname);
        toggle('blanket');
        toggle(windowname); 

    }
}

此代码仅首次显示您的弹出窗口。

您需要jQuery Cookie plugin