Help understanding Cookies

Discussion in 'JavaScript' started by xnarutogodx, Nov 22, 2009.

  1. #1
    Here's the code how dimitar christoff generously posted. What I need to know is actually how to use it. Putting it in <a href="http://en.site.com/" title="English"><strong>English</strong> - continue</a>

    var Cookie = {
        // wrapper for working with cookies.
        set: function(c_name, value, options) {
            options = (typeof(options) === "undefined") ? {
                duration: null, // in days, if null, session only
                path: null // relative to domain, if null, default from URI
            } : options;
    
            if (options.duration !== null) {
                var exdate = new Date();
                exdate.setDate(exdate.getDate()+options.duration);
            }
    
            document.cookie=c_name+ "=" +escape(value)+ ((options.duration===null) ? "" : ";expires="+exdate.toGMTString()) + ((options.path===null) ? "" : ";path="+options.path);
        },
        get: function(c_name) {
            if (document.cookie.length > 0) {
                var c_start = document.cookie.indexOf(c_name + "=");
                if (c_start !== -1) {
                    c_start = c_start + c_name.length+1;
                    var c_end = document.cookie.indexOf(";",c_start);
                    if (c_end === -1)
                        c_end = document.cookie.length;
                    return unescape(document.cookie.substring(c_start,c_end));
                }
            }
            return false;
        },
        remove: function(c_name) {
            this.set(c_name, "", {duration: -1, path: "/"});
        }
    }; // end Cookie wrapper
    PHP:
    To use it
    // set
    Cookie.set("language", "en", {path: "/", duration: 365});
    
    // read it into a var
    var language = Cookie.get("language");
    
    // remove it.
    Cookie.remove("language");
    PHP:
     
    xnarutogodx, Nov 22, 2009 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    what is the question?

    once you set it upon a click or whatever, you can always read it

    for example

    var language = Cookie.get("language"); // language => "en" always for any subsequent page.
     
    dimitar christoff, Nov 22, 2009 IP
  3. xnarutogodx

    xnarutogodx Peon

    Messages:
    64
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    // set
    Cookie.set("language", "en", {path: "/", duration: 365});
    
    // read it into a var
    var language = Cookie.get("language");
    
    // remove it.
    Cookie.remove("language");
    
    PHP:
    How do I use for example // set
    Cookie.set("language", "en", {path: "/", duration: 365})
    Do I put this in a hyperlink or anywhere in the page?Is this suppose to be in hyperlinks? Ex:<a href="something with cookies
    To remove it how do I put in a href?

    This is the format I'm going to have for the to language.
    <a href="http://en.mysite.com/" title="English"><strong>English</strong> - continue</a>
    Any help will be greatly appreciated.
     
    xnarutogodx, Nov 22, 2009 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    just code a function.

    you can also use inline js like so: <a href="blah" onclick="javascript:cookie.set('language', this.getAttribute('title'))" > ...
    or something (would record english into your cookie)

    although this is a bad practice. somehow i don't think this matters a lot in your case :)
     
    dimitar christoff, Nov 23, 2009 IP
  5. mrsmirf

    mrsmirf Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I am looking for a how to guide for cookies, because I own a site and would like to track my users. Or rather set up a referral system.:eek:
     
    mrsmirf, Nov 23, 2009 IP
  6. xnarutogodx

    xnarutogodx Peon

    Messages:
    64
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    How do I use for example // set
    Cookie.set("language", "en", {path: "/", duration: 365})
    Would it be :<a href="blah" onclick="javascript:cookie.set('language', {path: "/", duration: 365});" >
    What is this.getAttribute('title'))" > suppose to be?
     
    xnarutogodx, Nov 23, 2009 IP
  7. harit87

    harit87 Peon

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    harit87, Nov 23, 2009 IP
  8. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #8


    right. basically the value you assign to the cookie is the second parameter

    here is a practical piece of code that will check if the user has visited before, assign 'visited' to true or false dependent of the result.

    if they have NOT visited previously, set them a cookie which will change subsequent visits to use 'visited = true' instead:
    
    var visited = Cookie.get("visited");
    
    if (!visited) { // not visited, set the cookie
        // assign a cookie visited with value true || 1 that lasts 30 days:
        Cookie.set("visited", true, {duration: 30, path: "/"});
        // you can now do something for new visitors.
    }
    else {
        // they have visited before, do whatever you do to returning users
    }
    
    Code (javascript):
    the code above is not event / click driven.
     
    dimitar christoff, Nov 24, 2009 IP