Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> JavaScript >> Snippet

Write a cookie


Setting the cookie value of a documents is easy :

document.cookie = cookiestring

It 's the creation of the cookie string that takes some effort


Creating the text content of the cookie

Cookies -------------------------------------------------------

document.cookie = "cookiename=cookievalue"
x = document.cookie           //   x becomes "name1=value; name2=value2"   there is a space after the ;

Xarray = document.cookie.split ( "; " )     // note the space
Xarray[0] = "name1=value1"
Xarray[1] = "name2=value2'

no spaces / commas / semicolons allowed in cookie

document.cookie = "state" + escape ( "New York" // to handle spaces in cookie (*cannot have spaces otherwise)
X = unescape (document.cookie)    // reverses the escape action

document.cookie = "name=value" ;expires = GMT_string " // persistent cookies

cookie parameters :  expires path domain secure