Cookie

Basic cookie functions. Get, save, eat.

The cookie is a bit of a pseudo term in this context. "Cookies" will be saved in localStorage or sessionStorage, if available, to lessen request overhead – real cookies are being sent back and forth for each request. The storage method can be forced to real cookies if so desired.

In addition to regular cookies, Manipulator includes a simple way of mapping a cookie to a DOM node, allowing you to create a sort of Node memory, ie. if you want to remember the state of a node (open, closed, selected, entered text). These cookies are called nodeCookies.

Functions

Util.saveCookie

Definition

Name
Util.saveCookie
Syntax
Void = Util.saveCookie( String name, String value [, JSON _options] );

Description

Save cookie. dependencies session cookies via sessionStorage as default. If expires is not true, localStorage is used.

Use force option to force the use of real cookies. For real cookies you can use options to set detailed expiry and path.

Parameters

name
String Cookie name
value
String Cookie value
_options
JSON Additional cookie options
Options
path
path to save cookie with - default is current path. Only applicable for real cookies.
expires
Expiry date like, "Tue, 05-Apr-2020 05:00:00 GMT". Specific expiry date only applicable for real cookies. For localStorage/sessionStorage only expires = true/false can be used.
samesite
SameSite property to limit accessibility of cookie. Possible values are lax, strict and none. SameSite property only applicable for real cookies.

Return values

Void

Examples

u.saveCookie("cookie", "Chocolate Chip");

Saves cookie=Chocolate%20Chip as session cookie

u.saveCookie("cookie", "Oatmeal", {"expires":"Tue, 05-Apr-2020 05:00:00 GMT"});

Saves cookie=Oatmeal as permanent cookie, with expiry Tue, 05-Apr-2020 05:00:00 GMT

u.saveCookie("cookie", "Oatmeal", {"expires":true});

Saves cookie=Oatmeal as permanent cookie, with expiry Mon, 04-Apr-2020 05:00:00 GMT

Dependencies

JavaScript
  • encodeURIComponent
Manipulator

Nothing

Util.getCookie

Definition

Name
Util.getCookie
Syntax
String = Util.getCookie( String name );

Description

Get cookie.

Parameters

name
String Cookie name

Return values

String Cookie value for name

Examples

u.getCookie("cookie");

Returns value for cookie named "cookie".

Dependencies

JavaScript
  • String.match
  • encodeURIComponent
Manipulator

Nothing

Util.deleteCookie

Definition

Name
Util.deleteCookie
Syntax
Void = Util.deleteCookie( String name [, JSON _options] );

Description

Delete cookie by setting empty value and expire date 01.01.1970.

Parameters

name
String Cookie name
_options
JSON Additional cookie options
Options
path
path to save cookie with - default is current path

Return values

Void

Examples

u.deleteCookie("cookie");

Deletes cookie named "cookie"

Dependencies

JavaScript
  • encodeURIComponent
Manipulator

Nothing

Util.saveNodeCookie

Definition

Name
Util.saveNodeCookie
Syntax
Void = Util.saveNodeCookie( Node node, String name, String value [, JSON _options] );

Description

Save cookie with node reference. dependencies session cookies. Node reference is an autogenerated string, based on ID, nodeName, name attribute or className.

Node cookies are saved with path=/;

Parameters

node
Node Node to map cookie to
name
String Cookie name
value
String Cookie value
_options
JSON Additional node reference options
Options
ignore_classnames
List of classnames to ignore in node reference
ignore_classvars
Remove any classvars (x:y) classnames in node reference. Default false.

Return values

Void

Examples

none

Dependencies

JavaScript
  • JSON.parse
  • JSON.stringify
Manipulator
  • Util.cookieReference
  • Util.saveCookie

Util.getNodeCookie

Definition

Name
Util.getNodeCookie
Syntax
Mixed = Util.getNodeCookie( Node node, [String name] [, JSON _options] );

Description

Get cookie with node reference.

Parameters

node
Node Node to get referenced cookie of
name
String Optional Cookie name
_options
JSON Additional node reference options
Options
ignore_classnames
List of classnames to ignore in node reference
ignore_classvars
Remove any classvars (x:y) classnames in node reference. Default false.

Return values

If name is passed to function it returns String node referenced cookie value for name.

If no name is passed to function it returns JSON node referenced cookie object.

If no matching cookie is found, it returns false.

Examples

none

Dependencies

JavaScript
  • JSON.parse
Manipulator
  • Util.cookieReference
  • Util.getCookie

Util.deleteNodeCookie

Definition

Name
Util.deleteNodeCookie
Syntax
Void = Util.deleteNodeCookie( Node node, [String name] [, JSON _options] );

Description

Delete node referenced cookie object, or specific value.

Parameters

node
Node Node to get referenced cookie of
name
String Optional Cookie name
_options
JSON Additional node reference options
Options
ignore_classnames
List of classnames to ignore in node reference
ignore_classvars
Remove any classvars (x:y) classnames in node reference. Default false.

Return values

Void

Examples

none

Dependencies

JavaScript
  • JSON.parse
  • JSON.stringify
Manipulator
  • Util.cookieReference
  • Util.getCookie
  • Util.saveCookie

Files

Main file

  • u-cookie.js

Segment support files

  • none

Segment dependencies

desktop_edge
u-cookie.js
desktop_ie11
u-cookie.js
desktop
u-cookie.js
desktop_ie10
u-cookie.js
desktop_ie9
u-cookie.js
desktop_light
u-cookie.js + u-json-desktop_light.js
tablet
u-cookie.js
tablet_light
u-cookie.js
smartphone
u-cookie.js
mobile
not tested
mobile_light
not tested
tv
u-cookie.js + u-json-desktop_light.js
seo
not supported