|
4 | 4 |
|
5 | 5 | * client-side storage is `segregated by origin` (pages from one site can not read the data stored by pages from another site). |
6 | 6 | * Storage object is a `persistent associative array` that maps string keys to string values. |
| 7 | + |
| 8 | +#### localStorage and sessionStorage |
| 9 | + |
7 | 10 | * difference between `localStorage` and `sessionStorage` has to do with `'life-time'` and `'scope'` --> `'how long'` data is saved and for `'who'` the data is accessible to. |
8 | 11 | * note that `browsers only allows to store strings` (if we want to store any other type we need to encode and decode by ourself programmatically). |
9 | 12 | * data stored through localStorage is `permanent` (it does not expire and remains stored on the user's computer), until web app delete it or manually delete by the user. |
|
14 | 17 | * all documents with the same origin `share the same localStorage data` (regardless of the script that actually access localStorage). |
15 | 18 | * documents with different origins can `never` read or write or overwrite each other's data. |
16 | 19 |
|
17 | | -> localStorage is also scoped by `browser vendor` |
| 20 | +> localStorage is also scoped by `browser vendor`. |
18 | 21 |
|
19 | 22 | * for example we visit a site with Firefox and then visit again using Chrome. so the data stored in the visit with Firefox will not be accessible during the second visit by Chrome. |
20 | 23 |
|
@@ -56,4 +59,103 @@ var data = sessionStorage.getItem('key'); |
56 | 59 |
|
57 | 60 | * localStorage and storage event can `serve as a broadcast mechanism` by which a browser send a message to `all windows` that are currently visiting the same website. |
58 | 61 |
|
| 62 | +```js |
| 63 | + |
| 64 | +var person = { |
| 65 | + |
| 66 | + firstName : "Ehsan", |
| 67 | + lastName : "ZB", |
| 68 | + age : 34 |
| 69 | + |
| 70 | +}; |
| 71 | + |
| 72 | + |
| 73 | +function sendJSON(){ |
| 74 | + localStorage.personData = JSON.stringify(person); |
| 75 | + localStorage.setItem("nickName","Esi"); |
| 76 | +} |
| 77 | +``` |
| 78 | + |
59 | 79 | #### Cookies |
| 80 | + |
| 81 | +* is a small amount of named data `stored by the web browser`. it's associated with a particular web page or website. |
| 82 | +* cookies were originally designed for server-side programming. they are `implemented at the lowest-level` as an extension to the HTTP protocol. |
| 83 | + |
| 84 | +> server-side scripts can read/write cookie values. |
| 85 | +
|
| 86 | +* cookie data is automatically `transmitted between the web browser and the web server` |
| 87 | +* note that the `lifetime` and `scope` of each cookie can be individually specified with cookie attributes. |
| 88 | +* the values that cookies stored, store `last for the duration of the web browser session` (but `lost` when user `exits the browser`). |
| 89 | + |
| 90 | +> cookies `are not scoped to a single window` (default lifetime is the same as the entire browser process and `not the lifetime of any one window`). |
| 91 | +
|
| 92 | +* we need to tell the browser how long (in `seconds`) we would like `retain the cookie` (by specifying 'max-age' attribute, so once we specify the lifetime, the browser `store cookies in a file` and delete them only once they expire. |
| 93 | + |
| 94 | +> cookies are also scoped to document origin (same as local and session storages) and also scoped to document path. |
| 95 | +
|
| 96 | +* (this scope (`document path`) is configurable by 'path' and 'domain' attributes) |
| 97 | +* a cookie is `accessible to the web page that created it` and any other web pages in the same directory or any subdirectories of that directory. |
| 98 | +* 'www.example.com/catalog/index.html' creates a cookie -> that cookie is also available to /catalog/about.html, /catalog/featured/index.html,... |
| 99 | +* but `NOT accessible` to www.example.com/index.html, because the `path is different`. |
| 100 | + |
| 101 | +##### setting cookies |
| 102 | + |
| 103 | +> name=value;path=path;max-age=second; ... |
| 104 | +
|
| 105 | +* cookies `can not include` semicolon, commas, whitespace --> so we need to `encode` the value `before` storing in the cookie. |
| 106 | + |
| 107 | +##### reading cookies |
| 108 | + |
| 109 | +* string list of name=value pairs separated from each other by `;` |
| 110 | + |
| 111 | +> in order to make use of the `document.cookie` property, we must typically call the `split()` method to break it into individual name-value pairs. then we need to pass the cookie value to `decode` it and then use JSON.parse(). |
| 112 | +
|
| 113 | +```html |
| 114 | +<button onclick="sendJSON();">SAVE DATA</button> |
| 115 | +<button onclick="localStorage.clear();">CLEAR STORAGE</button> |
| 116 | +<button onclick="localStorage.removeItem('age');">REMOVE ITEM FROM STORAGE</button> |
| 117 | +<button onclick="setCookie('myName','Ehsan',1)">SET A COOKIE FOR ONE DAY</button> |
| 118 | +``` |
| 119 | + |
| 120 | +```js |
| 121 | +function setCookie(name,value,daystoLive){ |
| 122 | + |
| 123 | + var cookie = name + "=" + encodeURIComponent(value); |
| 124 | + |
| 125 | + if (typeof daystoLive === "number"){ |
| 126 | + cookie += "; max-age=" + (daystoLive*60*60*24); |
| 127 | + } |
| 128 | + |
| 129 | + document.cookie = cookie; |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +function getCookies(){ |
| 134 | + |
| 135 | + var cookies = {}; |
| 136 | + var all = document.cookie; |
| 137 | + |
| 138 | + if (all === ""){ |
| 139 | + return cookies; |
| 140 | + } |
| 141 | + |
| 142 | + var list = all.split("; "); |
| 143 | + |
| 144 | + // loop through name=value pairs |
| 145 | + for (var i = 0; i < list.length; i++){ |
| 146 | + |
| 147 | + var cookie = list[i]; |
| 148 | + var p = cookie.indexOf("="); |
| 149 | + var name = cookie.substring(0,p); |
| 150 | + var value = cookie.substring(p+1); |
| 151 | + |
| 152 | + // store the name and decoded value |
| 153 | + cookies[name] = decodeURIComponent(value); |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + return cookies; |
| 158 | +} |
| 159 | + |
| 160 | +console.log(getCookies()); // Object -> {myName:"Ehsan",.........} |
| 161 | +``` |
0 commit comments