Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 08-05-2005, 07:52 PM   #1 (permalink)
 
Newb Techie

Join Date: Jun 2005

Posts: 15

mlevit

Question Javascript problems

Code:
<SCRIPT LANGUAGE="JavaScript">




<!-- Begin
var expDays = 2; // number of days the cookie should last

var page = "upcoming_events.html";
var windowprops = "width=400,height=200,left = 40,top = 40,location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=no";

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>
The above is the code i have obtained (because i myself dont really know Javascript or how to set cookies etc)

There is also a "onLoad" statement in the body of the code that runs "checkCount()"

What the code should do: This code show check whether you have a cookie and if you dont, it will pop up with a window. Now the cookie should then be set for 2 days, and within those 2 days the pop up will not appear if you visit that same page.

Problem: When the code is executed, and you do not have a cookie, the pop up comes up and the cookie is set. If you visit again, there will not be another pop up, but a new cookie is set starting at the new date and time. So what this means is that there will never be another pop up. Because each time it checks for a new cookie it just sets another one for 2 days.

Can somebody please help me with this?
mlevit is offline  
Old 08-05-2005, 10:20 PM   #2 (permalink)
 
Monster Techie

Join Date: Jul 2003

Posts: 1,179

Emily is on a distinguished road

Send a message via AIM to Emily
Default

Before creating a new cookie, just check if the cookie with the same name already exists. Only create a new cookie if it doesn't already exist.
__________________
<a href=\"http://www.upstark.com\">www.upstark.com</a>
Emily is offline  
Old 08-06-2005, 01:46 AM   #3 (permalink)
 
Newb Techie

Join Date: Jun 2005

Posts: 15

mlevit

Default

You probably missed it, but i mentioned above that i obtained the code from a free website, because although i am doing Computer Science in uni i dont know Javascript.

So i was just wondering if someone here could spot the error in the script.
mlevit is offline  
Old 08-06-2005, 12:39 PM   #4 (permalink)
 
Monster Techie

Join Date: Jul 2003

Posts: 1,179

Emily is on a distinguished road

Send a message via AIM to Emily
Default

I don't know Javascript very well either. From what I can see which is probably not very much, in checkCount() it creates a cookie whether or not GetCookie() returns null. Just for kicks try replacing:

Quote:
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);
}
}
with:
Quote:
function checkCount() {
var count = GetCookie('count');
if (count == null) {
count=1;
SetCookie('count', count, exp);

window.open(page, "", windowprops);

}
}

__________________
<a href=\"http://www.upstark.com\">www.upstark.com</a>
Emily is offline  
Old 08-06-2005, 06:32 PM   #5 (permalink)
 
Newb Techie

Join Date: Jun 2005

Posts: 15

mlevit

Default

Thanks, it worked
mlevit is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On