Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Dec 2002
    Location
    Washington State
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation IF cookies,DoThis. ELSE,DoThis. - Using the IF Statement with cookies. (PayPal $2)

    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~
    ~ Isaac Lloyd ~
    Website Design & Development
    http://www.IsaacLloyd.com

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Are you trying to find a specific cookie?
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Ames, IA, USA
    Posts
    373
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Need more emoticons?
    Visit Catman's Private Stock

  • #4
    New Coder
    Join Date
    Dec 2002
    Location
    Washington State
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    *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~
    Last edited by isaaclloyd; 03-23-2005 at 11:52 PM.
    ~ Isaac Lloyd ~
    Website Design & Development
    http://www.IsaacLloyd.com

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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>

    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #6
    New Coder
    Join Date
    Dec 2002
    Location
    Washington State
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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~
    ~ Isaac Lloyd ~
    Website Design & Development
    http://www.IsaacLloyd.com

  • #7
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    No payment necessary, donate it to a good cause
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #8
    New Coder
    Join Date
    Dec 2002
    Location
    Washington State
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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~
    ~ Isaac Lloyd ~
    Website Design & Development
    http://www.IsaacLloyd.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •