storage.cookie¶
webix.storage.cookie helper.
Please look into the linked official documentation.
External references¶
Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | webix.storage.cookie = {
put:function(name, data, domain, expires ){
if(name && window.JSON){
document.cookie = name + "=" + escape(window.JSON.stringify(data)) +
(( expires && (expires instanceof Date)) ? ";expires=" + expires.toUTCString() : "" ) +
(( domain ) ? ";domain=" + domain : "" ) +
(( webix.env.https ) ? ";secure" : "");
}
},
_get_cookie:function(check_name){
// first we'll split this cookie up into name/value pairs
// note: document.cookie only returns name=value, not the other components
var a_all_cookies = document.cookie.split( ';' );
var a_temp_cookie = '';
var cookie_name = '';
var cookie_value = '';
var b_cookie_found = false; // set boolean t/f default f
for (var i = 0; i < a_all_cookies.length; i++ ){
// now we'll split apart each name=value pair
a_temp_cookie = a_all_cookies[i].split( '=' );
// and trim left/right whitespace while we're at it
cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
// if the extracted name matches passed check_name
if (cookie_name == check_name ){
b_cookie_found = true;
// we need to handle case where cookie has no value but exists (no = sign, that is):
if ( a_temp_cookie.length > 1 ){
cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
}
// note that in cases where cookie is initialized but no value, null is returned
return cookie_value;
}
a_temp_cookie = null;
cookie_name = '';
}
if ( !b_cookie_found ){
return null;
}
return null;
},
get:function(name){
if(name && window.JSON){
var json = this._get_cookie(name);
if(!json)
return null;
return webix.DataDriver.json.toObject(unescape(json));
}else
return null;
},
remove:function(name, domain){
if(name && this._get_cookie(name))
document.cookie = name + "=" + (( domain ) ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
},
clear:function(domain){
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++)
document.cookie = /^[^=]+/.exec(cookies[i])[0] + "=" + (( domain ) ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
};
|