src/cookie.js

Method expire

Parameters:

  • name must be a String.
    (Name of the cookie to expire)

  • domain must be a String.
    ([Optional] Domain to set the cookie for)

  • secure must be a Boolean.
    ([Optional] Make the cookie only accessible via SSL)

  • path must be a String.
    ([Optional] Path the cookie is for)

  • jar must be a String.
    ([Optional] Cookie jar, defaults to document.cookie)

Returns a String
(Name of the expired cookie)

expire : function ( name, domain, secure, path, jar ) { cookie.set( name, "", "-1s", domain, secure, path, jar ); return name; },

Method get

Gets a cookie

Parameters:

  • name must be a String.
    (Name of the cookie to get)

  • jar must be a String.
    ([Optional] Cookie jar, defaults to document.cookie)

Returns a Mixed
(Cookie or undefined)

get : function ( name, jar ) { return utility.coerce( cookie.list( jar )[name] ); },

Method list

Gets the cookies for the domain

Parameters:

  • jar must be a String.
    ([Optional] Cookie jar, defaults to document.cookie)

Returns an Object
(Collection of cookies)

list : function ( jar ) { var result = {}; if ( jar === undefined ) { jar = server ? "" : document.cookie; } if ( !string.isEmpty( jar ) ) { array.each( string.explode( jar, ";" ), function ( i ) { var item = string.explode( i, "=" ); result[item[0]] = utility.coerce( item[1] ); } ); } return result; },

Method set

Creates a cookie The offset specifies a positive or negative span of time as day, hour, minute or second

Parameters:

  • name must be a String.
    (Name of the cookie to create)

  • value must be a String.
    (Value to set)

  • offset must be a String.
    (A positive or negative integer followed by "d", "h", "m" or "s")

  • domain must be a String.
    ([Optional] Domain to set the cookie for)

  • secure must be a Boolean.
    ([Optional] Make the cookie only accessible via SSL)

  • path must be a String.
    ([Optional] Path the cookie is for)

  • jar must be a String.
    ([Optional] Cookie jar, defaults to document.cookie)

Returns an Undefined
(undefined)

set : function ( name, value, offset, domain, secure, path, jar ) { value = ( value || "" ) + ";"; offset = offset || ""; domain = typeof domain === "string" ? ( " Domain=" + domain + ";" ) : ""; secure = ( secure === true ) ? " secure" : ""; path = typeof path === "string" ? ( " Path=" + path + ";" ) : ""; var expire = "", span = null, type = null, types = ["d", "h", "m", "s"], regex = new RegExp(), i = types.length, cookies; if ( !string.isEmpty( offset ) ) { while ( i-- ) { utility.compile( regex, types[i] ); if ( regex.test( offset ) ) { type = types[i]; span = number.parse( offset, 10 ); break; } } if ( isNaN( span ) ) { throw new Error( label.error.invalidArguments ); } expire = new Date(); if ( type === "d" ) { expire.setDate( expire.getDate() + span ); } else if ( type === "h" ) { expire.setHours( expire.getHours() + span ); } else if ( type === "m" ) { expire.setMinutes( expire.getMinutes() + span ); } else if ( type === "s" ) { expire.setSeconds( expire.getSeconds() + span ); } } if ( expire instanceof Date) { expire = " Expires=" + expire.toUTCString() + ";"; } if ( !server ) { document.cookie = ( string.trim( name.toString() ) + "=" + value + expire + domain + path + secure ); } else { cookies = jar.getHeader( "Set-Cookie" ) || []; cookies.push( ( string.trim( name.toString() ) + "=" + value + expire + domain + path + secure ).replace( /;$/, "" ) ); jar.setHeader( "Set-Cookie", cookies ); } } };