
Untitled
By: a guest on
Jun 10th, 2012 | syntax:
None | size: 1.33 KB | hits: 15 | expires: Never
Javascript: how would I perform actions on the cookie object in the following function?
if (typeof String.prototype.trimLeft !== "function") {
String.prototype.trimLeft = function() {
return this.replace(/^s+/, "");
};
}
if (typeof String.prototype.trimRight !== "function") {
String.prototype.trimRight = function() {
return this.replace(/s+$/, "");
};
}
function getCookies() {
var c = document.cookie, v = 0, cookies = {};
if (document.cookie.match(/^s*$Version=(?:"1"|1);s*(.*)/)) {
c = RegExp.$1;
v = 1;
}
if (v === 0) {
c.split(/[,;]/).map(function(cookie) {
var parts = cookie.split(/=/, 2),
name = decodeURIComponent(parts[0].trimLeft()),
value = parts.length > 1 ? decodeURIComponent(parts[1].trimRight()) : null;
cookies[name] = value;
});
} else {
c.match(/(?:^|s+)([!#$%&'*+-.0-9A-Z^`a-z|~]+)=([!#$%&'*+-.0-9A-Z^`a-z|~]*|"(?:[x20-x7Ex80xFF]|\[x00-x7F])*")(?=s*[,;]|$)/g).map(function($0, $1) {
var name = $0,
value = $1.charAt(0) === '"'
? $1.substr(1, -1).replace(/\(.)/g, "$1")
: $1;
cookies[name] = value;
});
}
return cookies;
}
function getCookie(name) {
return getCookies()[name];
}