I used JavaScript to set a cookie when a user clicks on a pop-up notification, but after the browser is completely closed, the cookie expires and the notification is displayed again when navigating the page.
I want the user to be able to essentially say "Don't show it again . "
1) Is there a better way to do this, like storing data in db?
2) Can I set a cookie so that it does not expire when the window is closed?
Here is the code for setting cookies. If the cookie is not configured to "visit", it calls another function showLightBox().
$j(document).ready(function() {
$j(function() {
checkCookie();
});
function checkCookie() {
var hasVisit = getCookie("hasVisited");
if(hasVisit != "visited") {
showLightBox();
}
}
function getCookie(c_name) {
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
c_start = c_value.indexOf(c_name + "=");
if (c_start == -1)
c_value = null;
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
c_end = c_value.length;
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
function setCookie(cookieName, value) {
document.cookie = cookieName + "=" + value;
}
source
share