Cookie //JavaScript Repository

Description

Object to manage cookies.
Created: 2006.04.24

Code (Download)

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/geral/cookie [rev. #1]

Cookie = {
    isSupported: function(){
        return !!navigator.cookieEnabled;
    },
    exists: function(name){
        return document.cookie.indexOf(name + "=") + 1;
    },
    write: function(name, value, expires, path, domain, secure) {
        expires instanceof Date ? expires = expires.toGMTString()
        : typeof(expires) == 'number' && (expires = (new Date(+(new Date) + expires * 1e3)).toGMTString());
        var r = [name + "=" + escape(value)], s, i;
        for(i in s = {expires: expires, path: path, domain: domain})
            s[i] && r.push(i + "=" + s[i]);
        return secure && r.push("secure"), document.cookie = r.join(";"), true;
    },
    read: function(name){
        var c = document.cookie, s = this.exists(name), e;
        return s ? unescape(c.substring(s += name.length, (c.indexOf(";", s) + 1 || c.length + 1) - 1)) : "";
    },
    remove: function(name, path, domain){
        return this.exists(name) && this.write(name, "", new Date(0), path, domain);
    }
};

Example (Example)

<script type="text/javascript">
//<![CDATA[
var n = "TEST";
document.write(
    "Cookies supported: ", Cookie.isSupported(), "<br />",
    'Writing cookie "' + n + '" = ', Cookie.write(n, "1234567890"), "<br />",
    'Cookie exists "' + n + '" = ', Cookie.exists(n) != 0, "<br />",
    'Reading cookie "' + n + '" = ', Cookie.read(n), "<br />",
    'Removing cookie "' + n + '" = ', Cookie.remove(n), "<br />",
    'Cookie exists "' + n + '" = ', Cookie.exists(n) != 0
);

//]]>
</script>

Help

Methods

Cookie.isSupported(void): Boolean
Returns true only if the browser supports the writing, reading and remotion of cookies.
Cookie.write(name: String, value: String, [expires: Object = AT_END_OF_SESSION], [path: String = CURRENT_PATH], [domain: String = CURRENT_DOMAIN], [secure: Boolean = false]): Boolean
Writes the cookie to the browser.
name
name of the cookie
value
information to be stored on the browser
expires
defines the lifetime of the cookie, if empty, the cookie will be kept until the user closes the browser, can receive a Date object specifying the expiration date or a number indicating the amount of seconds that the cookie will be kept alive
path
defines the path (in terms of url) where the cookie will be accessible
domain
defines the domain where the cookie will be accessible
secure
indicates if the cookie will be transmitted through a secure HTTPS connection, when defined as true, the cookie will be stored only if the there's a secure connection
Cookie.exists(name: String): Boolean
Returns true if the cookie exists.
name
name of the cookie
Cookie.read(name: String): String
If the cookie exists, returns the value stored on it, otherwise returns an empty string.
name
name of the cookie
Cookie.remove(name: String, [path: String = CURRENT_PATH], [domain: String = CURRENT_DOMAIN]): Boolean
Removes the cookie.
name
name of the cookie
path
defines the path (in terms of url) where the cookie is be accessible
domain
defines the domain where the cookie is be accessible

Rank (Votes: 17)

3.71