Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 10th, 2012  |  syntax: None  |  size: 1.33 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Javascript: how would I perform actions on the cookie object in the following function?
  2. if (typeof String.prototype.trimLeft !== "function") {
  3.     String.prototype.trimLeft = function() {
  4.         return this.replace(/^s+/, "");
  5.     };
  6. }
  7. if (typeof String.prototype.trimRight !== "function") {
  8.     String.prototype.trimRight = function() {
  9.         return this.replace(/s+$/, "");
  10.     };
  11. }
  12. function getCookies() {
  13.     var c = document.cookie, v = 0, cookies = {};
  14.     if (document.cookie.match(/^s*$Version=(?:"1"|1);s*(.*)/)) {
  15.         c = RegExp.$1;
  16.         v = 1;
  17.     }
  18.     if (v === 0) {
  19.         c.split(/[,;]/).map(function(cookie) {
  20.             var parts = cookie.split(/=/, 2),
  21.                 name = decodeURIComponent(parts[0].trimLeft()),
  22.                 value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
  23.             cookies[name] = value;
  24.         });
  25.     } else {
  26.         c.match(/(?:^|s+)([!#$%&'*+-.0-9A-Z^`a-z|~]+)=([!#$%&'*+-.0-9A-Z^`a-z|~]*|"(?:[x20-x7Ex80xFF]|\[x00-x7F])*")(?=s*[,;]|$)/g).map(function($0, $1) {
  27.             var name = $0,
  28.                 value = $1.charAt(0) === '"'
  29.                           ? $1.substr(1, -1).replace(/\(.)/g, "$1")
  30.                           : $1;
  31.             cookies[name] = value;
  32.         });
  33.     }
  34.     return cookies;
  35. }
  36. function getCookie(name) {
  37.     return getCookies()[name];
  38. }