I am using this code to open an html pop up, to which is wish to add a “don’t show again” button that will store a preference in a cookie so that the pop up won’t be shown again
Why isn’t it working?
this is the line that should write the cookie:
document.cookie = "dontShow=1; path=/; expires=Wednesday, 01-Jan-2020 12:00:00 GMT; domain=.qpcftw.co.cc";
Update: the problem is that the cookie isn’t stored, and the “alert(document.cookie);” (see below) shows that the cookie is null (nothing is shown).
Here’s the full JS code:
/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!
/***************************/
var popupStatus = 0;
$(document).ready(function(){
if (document.cookie != "dontShow=1; path=/; expires=Wednesday, 01-Jan-2040 12:00:00 GMT; domain=.qpcftw.co.cc"){
$("#backgroundPopup").css({
"opacity": "0.75"
});
$("#backgroundPopup").fadeIn(1000);
$("#popupContact").fadeIn(1000);
popupStatus = 1;
}
//CLOSING POPUP
//Don't show again
$("#dontShow").click(function(){
document.cookie = "dontShow=1; path=/; expires=Wednesday, 01-Jan-2020 12:00:00 GMT; domain=.qpcftw.co.cc";
alert(document.cookie); //4debugging
disablePopup();
});
//Click the x event!
$("#popupContactClose").click(function(){
disablePopup();
});
//Click out event!
$("#backgroundPopup").click(function(){
disablePopup();
});
//Press Escape event!
$(document).keypress(function(e){
if(e.keyCode==27 && popupStatus==1){
disablePopup();
}
});
});
//disabling popup with jQuery magic!
function disablePopup(){
//disables popup only if it is enabled
if(popupStatus==1){
$("#backgroundPopup").fadeOut(500);
$("#popupContact").fadeOut(500);
popupStatus = 0;
}
}
Try this:
Fiddle Demo
Edit: Or, as a custom jQuery plugin:
Plugin version Fiddle
Hope this helps! 🙂