2009
May
26

What are cookies?

Cookie

Cookies are small amounts of data stored by the web browser. They allow you to store particular information about a user and retrieve it every time they visit your pages. Each user has their own unique set of cookies.

Cookies are typically used by web servers to perform functions such as tracking your visits to websites, enabling you to log in to sites, and storing your shopping cart.

document.cookie

Cookie
  1. document.cookie = "name=value; expires=date; path=path; domain=domain; secure";
Property Description
name=value This sets both the cookie's name and its value.
expires=date This optional values sets the date that the cookie will expire on. The date should be in the format returned by the toGMTString() method of the Date object. If the expires value is not given, the cookie will be destroyed the moment the browser is closed.
path=path This optional value specifies a path within the site to which the cookie applies. Only documents in this path will be able to retrieve the cookie. Usually this is left blank, meaning that only the path that set the cookie can retrieve it.
domain=domain This optional value specifies a domain within which the cookie applies. Only websites in this domain will be able to retrieve the cookie. Usually this is left blank, meaning that only the domain that set the cookie can retrieve it.
secure This optional flag indicates that the browser should use SSL when sending the cookie to the server. This flag is really used.

Set a Cookie

Cookie
  1. var COOKIE = {
  2. set: function(name, value, options) {
  3. if (!name) return;
  4. options = options || {};
  5. if (!options.expires) options.expires = 1;
  6. var date = new Date();
  7. date.setTime(date.getTime() + options.expires*24*60*60*1000);
  8. var expires = "; expires=" + date.toGMTString();
  9. var path = options.path ? '; path=' + (options.path) : '';
  10. var domain = options.domain ? '; domain=' + (options.domain) : '';
  11. var secure = options.secure ? '; secure' : '';
  12. document.cookie = [name, "=", encodeURIComponent(value), expires, path, domain, secure].join("");
  13. }
  14. };

Retrive a Cookie

Example
  1. var COOKIE = {
  2. get: function(name) {
  3. if (!name) return;
  4. var value = "";
  5. if (document.cookie && document.cookie != "") {
  6. var cookies = document.cookie.split(";");
  7. for (var i=0; i<cookies.length; i++) {
  8. var cookie = cookies[i];
  9. // Trim the space.
  10. while(cookie.charAt(0) == " ") cookie = cookie.substring(1, cookie.length);
  11. // Tell if this cookie string begins with the name we want.
  12. if (cookie.substring(0, name.length + 1) == (name + "=")) {
  13. value = decodeURIComponent(cookie.substring(name.length + 1));
  14. break;
  15. }
  16. }
  17. }
  18. return value;
  19. }
  20. };

Delete a Cookie

Example
  1. <code>
  2. var COOKIE = {
  3. del: function(name) {
  4. if (!name) return;
  5. this.set(name, "", -1);
  6. }
  7. };
  8. </code>

View Demo

Related Posts


回應 (Leave a comment)