3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
9 * @class Ext.state.CookieProvider
10 * @extends Ext.state.Provider
11 * The default Provider implementation which saves state via cookies.
14 var cp = new Ext.state.CookieProvider({
16 expires: new Date(new Date().getTime()+(1000*60*60*24*30)), //30 days
19 Ext.state.Manager.setProvider(cp);
21 * @cfg {String} path The path for which the cookie is active (defaults to root '/' which makes it active for all pages in the site)
22 * @cfg {Date} expires The cookie expiration date (defaults to 7 days from now)
23 * @cfg {String} domain The domain to save the cookie for. Note that you cannot specify a different domain than
24 * your page is on, but you can specify a sub-domain, or simply the domain itself like 'extjs.com' to include
25 * all sub-domains if you need to access cookies across different sub-domains (defaults to null which uses the same
26 * domain the page is running on including the 'www' like 'www.extjs.com')
27 * @cfg {Boolean} secure True if the site is using SSL (defaults to false)
29 * Create a new CookieProvider
30 * @param {Object} config The configuration object
32 Ext.state.CookieProvider = function(config){
33 Ext.state.CookieProvider.superclass.constructor.call(this);
35 this.expires = new Date(new Date().getTime()+(1000*60*60*24*7)); //7 days
38 Ext.apply(this, config);
39 this.state = this.readCookies();
42 Ext.extend(Ext.state.CookieProvider, Ext.state.Provider, {
44 set : function(name, value){
45 if(typeof value == "undefined" || value === null){
49 this.setCookie(name, value);
50 Ext.state.CookieProvider.superclass.set.call(this, name, value);
54 clear : function(name){
55 this.clearCookie(name);
56 Ext.state.CookieProvider.superclass.clear.call(this, name);
60 readCookies : function(){
62 var c = document.cookie + ";";
63 var re = /\s?(.*?)=(.*?);/g;
65 while((matches = re.exec(c)) != null){
66 var name = matches[1];
67 var value = matches[2];
68 if(name && name.substring(0,3) == "ys-"){
69 cookies[name.substr(3)] = this.decodeValue(value);
76 setCookie : function(name, value){
77 document.cookie = "ys-"+ name + "=" + this.encodeValue(value) +
78 ((this.expires == null) ? "" : ("; expires=" + this.expires.toGMTString())) +
79 ((this.path == null) ? "" : ("; path=" + this.path)) +
80 ((this.domain == null) ? "" : ("; domain=" + this.domain)) +
81 ((this.secure == true) ? "; secure" : "");
85 clearCookie : function(name){
86 document.cookie = "ys-" + name + "=null; expires=Thu, 01-Jan-70 00:00:01 GMT" +
87 ((this.path == null) ? "" : ("; path=" + this.path)) +
88 ((this.domain == null) ? "" : ("; domain=" + this.domain)) +
89 ((this.secure == true) ? "; secure" : "");