Cookies help!

Discussion in 'JavaScript' started by xnarutogodx, Oct 21, 2009.

  1. #1
    Okay First of all hi everyone from digitalpoint.Next of all I want to know If any of you guys know how to have cookies like Netlog. I'm going to have basically same way. English, Spanish, etc... And when you click on something it takes you the url ex:en.netlog.com. Then If you want to change language you go at the top and click on the languages it resets the cookies and takes you the front page. I'm a newbie at cookies and wondering if any of you guys can help.
     
    xnarutogodx, Oct 21, 2009 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    here is my cookie wrapper:
    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:

    
    // set
    Cookie.set("language", "en", {path: "/", duration: 365});
    
    // read it into a var
    var language = Cookie.get("language");
    
    // remove it.
    Cookie.remove("language");
    PHP:
    this has been posted before, you should look first before asking.
     
    dimitar christoff, Oct 22, 2009 IP