Pop-ups With Cookies

pop-ups cookies popups

Pop-ups are combined with cookies to prevent the pop-up window from opening again to a visitor that has already seen it.

Ideally, you want your pop-up on every page of your site, because visitors will enter and exit through different pages. Unfortunately, doing that using a standard pop-up script is going to annoy people. Why?

Because a pop-up window will open every time they move from one page to another!


Adding Cookies to Your Pop-ups

A cookie is a small data file placed into the visitor's Web browser. Cookies are used to store various kinds of user-specific information that enable a Web site to distinguish one visitor from another. It could be a logon password, affiliate ID, customer ID, etc. In our case, we only need to record when the pop-up is allowed to appear to this visitor again.

The pop-up script below incorporates a function to check for a particular cookie in the visitor's browser. If the cookie isn't found, the script will create it, and simultaneously fire the pop-up window. If the cookie is located, the script will use the data stored there to decide wether or not it should open the pop-up.

Imagine a new visitor arriving at a page on your site. Her browser doesn't yet have your cookie, so the pop-up window opens and a cookie is written. When she moves on to another page, the pop-up script on this page also checks for your cookie, and finds the one created earlier. The cookie tells the script that your visitor has already seen the pop-up window, and that not enough time has passed to allow it to display the pop-up to her again. The same thing happens on every subsequent page she visits

She will never see the pop-up again until the amount of time you specify (in the script) has elapsed. That's usually one day, but if you have a lot of daily repeat traffic, you might want to set a longer interval.

Due to the more complex nature of this script, I suggest you become familiar with standard pop-ups first.


Pop-up Script With Cookies That Opens When Visitors Enter A Page

Place the following script between the <HEAD> and </HEAD> tags:

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
var expDays = 1; / number of days the cookie should last
var page = "subscribe.html"; /URL to the pop-up
var windowprops = "width=200,height=200,location=no,
toolbar=no,menubar=no,scrollbars=no,resizable=no"; / variables
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>


And add the following into your <BODY> tag:

onLoad="checkCount()"


Your <BODY> tag should then look something like this:

<BODY bgcolor="#ffffff" text="#000000" onLoad="checkCount()">

 

Pop-up Script With Cookies That Opens When Visitors Leave A Page

Just change the script above so that the pop-up opens when your visitor leaves, rather than when they enter the page. It's easy!

In your <BODY> tag simply replace "onLoad" with "onUnload" like this:

onUnload="checkCount()"

 

This is too hard – I want to use a PopUp Maker instead!

 

[Pop-up Windows] [Using Pop-up Ads] [Pop-up Scripts] [Exit Pop-ups] [Cookie Pop-ups]

[Pop-under Windows] [Pop-under Scripts] [Pop-up Maker] [Pop-Up Software]