Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use cookies to create popup 3 times then stop forever

Status
Not open for further replies.

BunBo900

MIS
Dec 11, 2009
50
0
0
US
Hello:

I am trying to have a popup on my site using cookies ..like 3 times or so then stop forever. The code below is only popup once...How can I change the code below to have it popup whenever the user returns to the page the first 3 times?

Thank you.

****************************************************
****************************************************

var expDays = 1; // number of days the cookie should last

var page = "only-popup-once.html";
var windowprops = "width=300,height=200,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes";

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);
}
}

window.onload=checkCount;
 
Hi

Logic error : you call [tt]open()[/tt] only when no cookie exists. [highlight]Move it[/highlight] after the [tt]if[/tt] and [highlight pink]add it the condition[/highlight] to check the count :
Code:
[b]function[/b] [COLOR=darkgoldenrod]checkCount[/color][teal]()[/teal] [teal]{[/teal]
  [b]var[/b] count [teal]=[/teal] [COLOR=darkgoldenrod]GetCookie[/color][teal]([/teal][green][i]'count'[/i][/green][teal]);[/teal]
  [b]if[/b] [teal]([/teal]count [teal]==[/teal] [b]null[/b][teal])[/teal] [teal]{[/teal]
    count[teal]=[/teal][purple]1[/purple][teal];[/teal]
  [teal]}[/teal] [b]else[/b] [teal]{[/teal]
    count[teal]++;[/teal]
  [teal]}[/teal]

  [highlight paleturquoise][COLOR=darkgoldenrod]SetCookie[/color][teal]([/teal][green][i]'count'[/i][/green][teal],[/teal] count[teal],[/teal] exp[teal]);[/teal][/highlight]

  [highlight pink][b]if[/b] [teal]([/teal]count[teal]<=[/teal][purple]3[/purple][teal])[/teal][/highlight]
    [highlight]window[teal].[/teal][COLOR=darkgoldenrod]open[/color][teal]([/teal]page[teal],[/teal] [green][i]""[/i][/green][teal],[/teal] windowprops[teal]);[/teal][/highlight]
[teal]}[/teal]
Note that I also [highlight paleturquoise]moved the SetCookie()[/highlight] call after the [tt]if[/tt] as it had identical calls on both branches.

Another logic error : you set the cookie to expire in 1 day. So if the visitor returns the next day, the counter cookie will be vanished in meatime and popup counting will start again.

Next time please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags.

Feherke.
 
I think you misunderstood my question.

I just tried your code and all 3 popups come up at the same time when I visit the page (index.html). What I would like to accomplish is each time the user comes to the index.html page a popup appears. The first 3 times the user visits index.html will see popup and after that no more popups. It is like a welcome message...

Thanks you pointing out the expiration date:
I will set it to 10 years

var expDays = 3000;

 
Hi

Unless explicitly stated otherwise, I test the code I post. The above one worked as expected : 1 popup on each visit the first 3 times, then nothing.

If it not works like that for you, the problem is elsewhere. If you need further help with the debugging, post the rest of the code.

One tip in meantime : you included the JavaScript code in every HTML document, including the one which appears in the popup window.


Feherke.
 
Yes, it works as expected. I made a mistake on the html page.

Thank you for your time!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top