Ticket #167 (new enhancement)

Opened 2 years ago

Last modified 1 year ago

MochiKit.Cookie or MochiKit.Storage

Reported by: beau@hartshornesoftware.com Assigned to: beau@hartshornesoftware.com
Priority: normal Milestone: MochiKit 1.5
Component: component1 Version:
Severity: normal Keywords:
Cc:

Description

Provide a nice interface to browser cookies.

Change History

08/25/06 16:50:59 changed by beau@hartshornesoftware.com

  • owner changed from somebody to beau@hartshornesoftware.com.

09/14/06 09:19:05 changed by sky

these are in my tool box. I'm commenting to ensure you consider the case of not enough memory for setting the cookie, which can cause bad rendering of overloaded pages.

function setCookie(name, value, expires, path, domain, secure) {   
    var val = escape(value);
    if (document.cookie.length + val.length < MAX_COOKIE_SIZE) {
	throw new MochiKit.Base.NamedError("MochiKit.Cookies.NoMemory")
    }
    document.cookie= name + "=" + val +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

function getCookie(name) {
    	var dc = document.cookie;
    	var prefix = name + "=";
    	var begin = dc.indexOf("; " + prefix);
    	if (begin == -1) {
        	begin = dc.indexOf(prefix);
        	if (begin != 0) return null;
    	} else {
        	begin += 2;
    	}
    	var end = document.cookie.indexOf(";", begin);
    	if (end == -1) {
        	end = dc.length;
    	}
    	return unescape(dc.substring(begin + prefix.length, end));
}

01/20/07 18:55:00 changed by beau@hartshornesoftware.com

  • milestone set to MochiKit 1.5.