I’m adding the following code to add an overlay
$(function () {
{
$('#overlay').fadeIn('fast', function () {
$('#overlaybox').animate({ 'top': '90px' }, 500);
//$('#box').css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
$('#overlaybox').css("left", (($(window).width() - $('#overlaybox').outerWidth()) / 2) + $(window).scrollLeft() + "px");
$('#nabshow').fadeOut('fast');
})
}
});
I found the following code to open a popup based on availability of cookie:
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays===null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x===c_name)
{
return unescape(y);
}
}
}
function showPopupOnce() {
var hasSeenPopup = myGetCookie("has_seen_popup");
if (hasSeenPopup === null || hasSeenPopup === ""){
// the user has never seen the popup, so show him!
window.open("http://mywebsite.com/popup.html", "myPopupWindow");
}
// either way, set the cookie so the user will never see the window again
mySetCookie("has_seen_popup", "true", 365); // 365 days = 1 year
}
<body onLoad="showPopupOnce();">
What is the changes I need to do in my overlay code to ensure it gets shown only once.
I will add the get cookie and setcookie functions, then what do I do:
Thanks
Arnab
You’ve done everything already! All you need to do it edit your
showPopupOnce()code.Right now you have the overlay code in an anonymous function. So that’ll run every time the page is loaded. Once you move the code over to the showPopup function inside the case where there is no cookie; it will do what you want it to do.
Let me know if you need any clarification / help.