...

View Full Version : IF cookies,DoThis. ELSE,DoThis. - Using the IF Statement with cookies. (PayPal $2)



isaaclloyd
03-23-2005, 11:23 PM
I've been working on trying to get something like this to work for a few days now and it's really bugging me. Here's an idea of what I got:


if(document.cookie.indexOf("mycookie")!=-1){
alert("There is a Cookie!");
}

else {
alert("There are no Cookies!");
}



This doesn't work though. It just says "There us a Cookie!" everytime, even when there is no cookies for the page. I'll send $2 PayPal to anyone who can help me get this script working. Thanks for your time.


~Isaac Lloyd~

Mr J
03-23-2005, 11:42 PM
Are you trying to find a specific cookie?

Catman
03-23-2005, 11:47 PM
I don't see anything wrong with the if statement, so I have to ask: Are you absolutely sure there's no cookie? Unless you've specifically deleted the cookie in question, or the cookie is a session cookie which dies when the browser is closed, it may still be hanging around.

An easy way to verify: just drop in an alert(document.cookie) before the if statement. If that shows up blank, your no cookie message should appear.

isaaclloyd
03-23-2005, 11:49 PM
*Edit* The script above doesn't work, there are no cookies and it still does the first line.

The cookie that will be created (upon clicking on an image) will be:


function createLuckyCookie() {
if(document.cookie.indexOf("luckycookie") == -1){
when= new Date()
when.setHours(when.getHours()+1)
expires=when.toGMTString()
document.cookie = "luckycookie=lucky;expires="+expires+";path=/"
}
}



I am trying to make it so people can click on an image once, and it stores a cookie. If they click on it again it detects that they have a cookie and returns false or stops.

So I need to detect that cookie, or any cookies for the domain and IF cookies, Alert() IF no cookies, continue & write cookie.

Thanks for your time, and the person who helps complete the final script I will PayPal $2 to.


~Isaac Lloyd~

Mr J
03-23-2005, 11:58 PM
Please try the following



<script type="text/javascript" language="javascript">
<!--
days=1 // number of cookie days
cookie_name="my_cookie"

function get_cookie (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 get_cookie_val (j)
i = document.cookie.indexOf(" ", i) + 1
if (i == 0) break
}
return null
}

function get_cookie_val(offset) {
var endstr = document.cookie.indexOf (";", offset)
if (endstr == -1)
endstr = document.cookie.length
return unescape(document.cookie.substring(offset, endstr))
}

function set_cookie (name, value,expires) {
var argv = set_cookie.arguments
var argc = set_cookie.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 delete_cookie(name,path,domain){
document.cookie = name + "=" +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
"; expires=Thu, 01-Jan-00 00:00:01 GMT";
}


function chk_cookie() {
var count = get_cookie(cookie_name)

if (count == null) {
count=1
var exp = new Date()
exp.setTime(exp.getTime() + (days*24*60*60*1000))
set_cookie(cookie_name,count, exp)
}
else{
do_this()
}

}

function do_this(){
alert("HI")
}

// -->
</script>


<img src="pic.jpg" onclick="chk_cookie()">

<P><a href="#null" onclick="delete_cookie('my_cookie')">Delete Cookie</a>

isaaclloyd
03-24-2005, 12:09 AM
Thank you oh so much Mr. J. I wish I didn't spend these past 2 days and all of last night trying to figure that one out. Please send me your PayPal email address. Thanks for your time everyone, and have a great day.


~Isaac Lloyd~

Mr J
03-24-2005, 12:22 AM
No payment necessary, donate it to a good cause :thumbsup:

isaaclloyd
03-24-2005, 12:25 AM
k.... If thats what you want... I was just about to up the price if you could help me with 2 other scripts. I'll do a different post for those though. Thanks for your help Mr. J. and let me know if you change your mind ;)


~Isaac Lloyd~



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum