简体   繁体   English

Javascript:弹出框,如果单击“不显示”则不显示

[英]Javascript: Popup box, don't show if clicking “Don't Show”

much like magazine websites, there's often a popup box that you're greeted with on visit asking to subscribe. 就像杂志网站一样,访问时经常会弹出一个对话框,要求您订阅。
I basically have the same thing but I want it to not show up for a certain amount of time, let's say a week, if they click "Don't show". 我基本上有相同的东西,但是我希望它在某个时间段内(例如,如果他们单击“不显示”)在一段时间内不显示。

fid: https://jsfiddle.net/jzhang172/jh3L5kox/ fid: https : //jsfiddle.net/jzhang172/jh3L5kox/

 window.onload=func1(); function func1(){ $('.pop-up').fadeIn(); } $('.nope').click(function(){ $('.pop-up').fadeOut(); }); 
 body,html{ padding:0; margin:0; } *{ box-sizing:border-box; } .overlay{ position:absolute; top:0; left:0; right:0; bottom:0; background:black; } .pop-up { height: 40vw; width: 40vw; position: fixed; top: 50%; left: 50%; margin-left: -20vw; margin-top: -20vw; background: rgba(255, 255, 255, 0.83); text-align: center; border:1px solid black; box-shadow: 1px 1px 39px 5px #584B4B; } .nope{ text-decoration:underline; cursor:pointer; font-size:13px; } .pop-up{ display:none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="overlay"> <div class="pop-up"> <h2>Subscribe to be Awesome</h2> <span class="nope">Don't be awesome</span> </div> </div> 

You can use cookies to show/hide the popup. 您可以使用cookie来显示/隐藏弹出窗口。

1- Set a cookie, when user clicks to close the popup. 1-设置cookie,当用户单击以关闭弹出窗口时。

$('.nope').click(function(){
 document.cookie="mycookie=my_cookie_value; expires=Thu, 28 Apr 2016 12:00:00 UTC; path=/"; 
 $('.pop-up').fadeOut();
});

2- Get the cookie on document load. 2-在文档加载时获取cookie。 if your cookie exists, hide the popup else show 如果您的Cookie存在,则隐藏弹出窗口,否则显示

function func1(){   
var x = document.cookie; //this will give you all the cookies set on the browser. Check if your cookie exists
if(!cookie_exist){
    $('.pop-up').fadeIn();
   }
}

You need to check your cookie every time the browser loads. 每次加载浏览器时,您都需要检查Cookie。 Replace the variable and if condition with your values. 用您的值替换变量和if条件。 For more information on cookies visit http://www.w3schools.com/js/js_cookies.asp 有关Cookie的更多信息,请访问http://www.w3schools.com/js/js_cookies.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM