Setting and retrieving a cookie
|document.cookie||Read/write property that stores and persists data in a semicolon (;) delimited format|
Think of document.cookie as the familiar variable, but with a twisted, persistent personality: Below we store a string into it, and examine what comes out:
document.cookie is a "public facility", with its usage shared by all scripts on all sites that access it. Due to this, the property often contains more values than you had stored into it, the other values being from other sites. When this is the case, each value is separated by a semicolon. All of these values are persisted until the browser is closed.
Retrieving data from the cookie
Ok, so everyone can take a bite out of document.cookie; how do you locate your piece and grab it? Herein lies the bulk of your work when working with session only cookies- retrieving what was put into it.
The following 2 steps are involved in most techniques used to get your data out of a session cookie:
Quite a mess, all the reason to read on...
-Get cookie routine
With respect to the topic at hand- retrieving data from a cookie- an excellent prebuilt routine to use is the following:
With it in place, to extract your data from document.cookie, simply input the data's name as parameter:
Should the corresponding value not exist, the routine returns an empty string.
We now know how to both store and retrieve data from a session-only cookie. But what good is that?