使我的javaScript弹出窗口只出现一次

时间:2014-05-21 06:21:25

标签: javascript cookies

我正在创建一个JavaScript弹出窗口。代码如下。

HTML:

    <div id="ac-wrapper" style='display:none' onClick="hideNow(event)">
    <div id="popup">
        <center>
             <h2>Popup Content Here</h2> 
            <input type="submit" name="submit" value="Submit" onClick="PopUp('hide')" />
        </center>
    </div>
</div>

CSS:

    #ac-wrapper {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: url("images/pop-bg.png") repeat top left transparent;
    z-index: 1001;
}
#popup {
    background: none repeat scroll 0 0 #FFFFFF;
    border-radius: 18px;
    -moz-border-radius: 18px;
    -webkit-border-radius: 18px;
    height: 361px;
    margin: 5% auto;
    position: relative;
    width: 597px;
}

剧本:

function PopUp(hideOrshow) {
    if (hideOrshow == 'hide') document.getElementById('ac-wrapper').style.display = "none";
    else document.getElementById('ac-wrapper').removeAttribute('style');
}
window.onload = function () {
    setTimeout(function () {
        PopUp('show');
    }, 0);
}

function hideNow(e) {
    if (e.target.id == 'ac-wrapper') document.getElementById('ac-wrapper').style.display = 'none';
}

jsFiddle链接:

http://jsfiddle.net/K9qL4/2/

问题:

上面的脚本运行正常,但我需要让popUp只在我的页面上出现一次。 即,当用户关闭弹出窗口时,它不会出现,直到用户重新启动浏览器或清除其缓存/ cookie。

我尝试使用下面的Cookie脚本,但它对我不起作用。

<SCRIPT LANGUAGE="JavaScript">



<!-- Begin
var expDays = 1; // number of days the cookie should last

var page = "myPage.html";
var windowprops = "width=300,height=200,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes";

function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function amt(){
var count = GetCookie('count')
if(count == null) {
SetCookie('count','1')
return 1
}
else {
var newcount = parseInt(count) + 1;
DeleteCookie('count')
SetCookie('count',newcount,exp)
return count
   }
}
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function checkCount() {
var count = GetCookie('count');
if (count == null) {
count=1;
SetCookie('count', count, exp);

window.open(page, "", windowprops);

}
else {
count++;
SetCookie('count', count, exp);
   }
}
//  End -->
</script>

4 个答案:

答案 0 :(得分:3)

我在这种情况下最好使用localStorage而不是cookie。 localStorage具有更直观的界面,用户无法限制 要使用此功能。我已经改变了你的代码。

function PopUp(hideOrshow) {
    if (hideOrshow == 'hide') {
        document.getElementById('ac-wrapper').style.display = "none";
    }
    else  if(localStorage.getItem("popupWasShown") == null) {
        localStorage.setItem("popupWasShown",1);
        document.getElementById('ac-wrapper').removeAttribute('style');
    }
}
window.onload = function () {
    setTimeout(function () {
        PopUp('show');
    }, 0);
}


function hideNow(e) {
    if (e.target.id == 'ac-wrapper') document.getElementById('ac-wrapper').style.display = 'none';
}

这是jsFiddle的工作。 http://jsfiddle.net/zono/vHG7j/

祝你好运。

答案 1 :(得分:2)

由于其他原因,我使用了本地存储而不是cookie

但是,我添加了比较,并检查了您是否要显示它(还添加了一个重置​​按钮供您轻松​​测试)

小提琴是:http://jsfiddle.net/K9qL4/8/

    function PopUp(hideOrshow) {
        if (hideOrshow === 'hide') {
            document.getElementById('ac-wrapper').style.display = "none";
        }
        else if(localStorage.getItem("popupWasShown") !== "1" && hideOrshow === 'show') {
            document.getElementById('ac-wrapper').removeAttribute('style');
            localStorage.setItem("popupWasShown", "1");
        }
    }
    window.onload = function () {
        setTimeout(function () {
            PopUp('show');
        }, 1000);
    }


    function hideNow(e) {
        if (e.target.id == 'ac-wrapper') {
            document.getElementById('ac-wrapper').style.display = 'none';
            localStorage.setItem("popupWasShown", "1");
        }
    }

document.getElementById("reset").onclick = function() {
    localStorage.setItem("popupWasShown", "3");
}

答案 2 :(得分:1)

在重新启动浏览器之前不显示此内容 - 使用本地存储

localStorage.setItem("setted",true);
localStorage.getItem("setted");

FIDDLE

在清除缓存\ cookie使用cookie之前不显示

document.cookie = "setted=true";
document.cookie.indexOf("setted=true")!=-1

FIDDLE

答案 3 :(得分:0)

我们对会话存储进行了少许修改,以便在每次加载页面时(每天几次或在新的窗口/标签中)加载弹出窗口:

    else  if(sessionStorage.getItem("popupWasShown") == null) {
        sessionStorage.setItem("popupWasShown",1);
        document.getElementById('ac-wrapper').removeAttribute('style');
    }

完整代码:

function PopUp(hideOrshow) {
    if (hideOrshow == 'hide') {
        document.getElementById('ac-wrapper').style.display = "none";
    }
else  if(sessionStorage.getItem("popupWasShown") == null) {
        sessionStorage.setItem("popupWasShown",1);
        document.getElementById('ac-wrapper').removeAttribute('style');
    }
}
window.onload = function () {
    setTimeout(function () {
        PopUp('show');
    }, 0);
}


function hideNow(e) {
    if (e.target.id == 'ac-wrapper') document.getElementById('ac-wrapper').style.display = 'none';
}