Last updated: 31 October 2010Cookie
By: Dhryn
Description
Using this class you can easily and quickly access cookies through JavaScript. With extra methods to help setting and getting objects much easier.| Methods |
|---|
| get(name:String, prefix:String = null):String Use to get a stored cookie by name |
| getByPrefix(removePrefix:Boolean = false, prefix:String = null):Array Returns an object of all cookies found with the passed prefix. |
| getPrefix():String Returns the current prefix stored in Cookie. |
| isset(name:String):Boolean Find if a given cookie has already been set. |
| set(name:String, value:String, days:Number = 1, overwrite:Boolean = true, prefix:String = null):Boolean Use to set or change a cookie |
| setArrays(names:Array, values:Array, days:Number = 1, overwrite:Boolean = true, prefix:String = null):Boolean Pass two arrays, the first with the names of cookies and the second with the corresponding cookie values, to be set. |
| setObject(object:Object, days:Number = 1, overwrite:Boolean = true, prefix:String = null):Boolean Pass an entire object to be saved as Cookies. |
| setPrefix(prefix:String):void |
| unset(unset:String):Boolean Used to remove a cookie. |
Method Detail
get()
public function get(name:String, prefix:String = null):StringSearches through the cookies, trying to match the name passed to it. If successful, the value of the cookie will be returned; otherwise, null is returned.
A prefix can be passed to it which will be used for this search only. Alternatively you can set a prefix to be used by all searches using Cookie.setPrefix(). If a prefix is set and you pass one to the method, the passed prefix will be used; the set prefix will not be replaced or removed using this method.
Parameters
name:String - The name used when searching through the stored cookies
prefix:String = null - If set, this prefix will be used instead of the one stored in Cookie.prefix. Setting this will not change the value of Cookie.prefix
Returns
String - Returns value of cookie if found; otherwise, null is returned.
Examples
If you want to know whether or not a Cookie is set, you should use Cookie.isset();. However, if you want to check if the cookie was set before trying to use it, you can use the following.
| 1 2 3 4 |
var name = Cookie.get('name');
if(name !== false) {
//Code to use Cookie here.
}
|
|---|
getByPrefix()
public function getByPrefix(removePrefix:Boolean = false, prefix:String = null):ArrayParameters
getPrefix()
public function getPrefix():StringReturns
String - Returns the current prefix. If a prefix isn't set, a blank string will be returned.
isset()
public function isset(name:String):BooleanParameters
name:String - The name of the cookie you want to check.
Returns
Boolean - True will be returned if the given cookie name was found; otherwise, false will be returned.
set()
public function set(name:String, value:String, days:Number = 1, overwrite:Boolean = true, prefix:String = null):BooleanParameters
name:String - The name used when setting the cookie.
value:String - The value used for the cookie being set.
days:Number = 1 - The number of days before the cookie expires.
overwrite:Boolean = true - Specify whether of not the cookie should be overwrite if found with the same name.
prefix:String = null - If set, this prefix will be used instead of the one stored in Cookie; otherwise, the stored prefix will be used.
Returns
Boolean - Returns true on success; otherwise, false.
setArrays()
public function setArrays(names:Array, values:Array, days:Number = 1, overwrite:Boolean = true, prefix:String = null):BooleanParameters
names:Array - Pass an array of names to be used when setting the cookies.
values:Array - Pass an array of values to be used when setting the cookies.
days:Number = 1 - The length of time the cookie should last for.
overwrite:Boolean = true - If a cookie by the same name is found, should it be overwritten.
prefix:String = null
Returns
Boolean - True will be returned on success, false will be returned otherwise.
Examples
| 1 2 3 4 5 6 |
var names = ['one', 'two', 'three'];
var values = [1, 2, 3];
Cookie.setArrays(names, values);
alert(Cookie.get('two'));
|
|---|
setObject()
public function setObject(object:Object, days:Number = 1, overwrite:Boolean = true, prefix:String = null):BooleanThe object variable names will be used as the cookie names along with a prefix if given or the prefix saved in the object, Cookie.
Parameters
object:Object - Pass an object of values to be saved as cookies.
days:Number = 1 - The length of time the cookie should last for.
overwrite:Boolean = true - If a cookie by the same name is found, should it be overwritten.
prefix:String = null - If set, this prefix will be used instead of the one stored in Cookie; otherwise, the stored prefix will be used.
Returns
Boolean - True will be returned on success, false will be returned otherwise.