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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23775440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:32:43  来源:igfitidea点击:

Making my javaScript popup to appear only once

javascriptcookies

提问by Nitesh

I am creating a JavaScript popup. The code is as below.

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

The HTML:

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>

The CSS:

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

The Script:

剧本:

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

The jsFiddle Link:

jsFiddle 链接:

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

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

The Issue:

问题:

The above script works fine, but I need to make the popUp to appear only once on my page. i.e, when the user closes the popup, it should not appear until the user restarts the browser or clears his cache/cookie.

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

I tried using the below cookie script, but it does not work for me.

我尝试使用下面的 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>

回答by Georgi Naumov

I thing in this case is better to use localStorage instead cookie. localStorage have a more intuitive interface and user cannot restrict this feature to be used. I have changed your code.

我在这种情况下最好使用 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';
}

Here is working jsFiddle. http://jsfiddle.net/zono/vHG7j/

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

Best regards.

最好的祝福。

回答by deadulya

To not show this untill restart browser - use local storage

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

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

FIDDLE

小提琴

To not show untill clear cache\cookie use cookies

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

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

FIDDLE

小提琴

回答by DrogoNevets

I've used local storage instead of cookie for the reason mentioned otherwise

由于另外提到的原因,我使用了本地存储而不是 cookie

however, I have added the comparison, and checked that you want to show it (also added a reset button for you to test easily)

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

fiddle is: http://jsfiddle.net/K9qL4/8/

小提琴是: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");
}

回答by Fabian ?uis Gisler

We have slightly modified the code with session storage in order to load the popup whenever the page is loaded (several times per day or in a new window/tab):

我们稍微修改了带有会话存储的代码,以便在加载页面时加载弹出窗口(每天几次或在新窗口/选项卡中):

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

Full code:

完整代码:

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