1 2 (function ($$) { 3 4 "use strict"; 5 6 var module = (function() { 7 8 function isSecure() 9 { 10 return window.location.protocol === 'https:'; 11 } 12 13 /** 14 * @description Create a cookie 15 * @param {String} name Cookie name 16 * @param {String} value Cookie value 17 * @param {Integer} [days] Number of days for the cookie to remain active. 18 If not provided, the cookie never expires 19 */ 20 function set(name, value, days) { 21 var expires = "", date; 22 if (days) { 23 date = new Date(); 24 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); 25 expires = "; expires=" + date.toGMTString(); 26 } 27 else { 28 expires = ""; 29 } 30 document.cookie = name + "=" + value + expires + "; path=/" + ((isSecure() === true) ? "; secure" : ""); 31 } 32 33 /** 34 * @description Get the cookie with the specified name 35 * @param {String} name The name of the cookie to retrieve 36 * @returns The value of the cookie if the name is found, otherwise null 37 */ 38 function get(name) { 39 var nameEQ, ca, c, i; 40 41 if ($$.isUndefined(name)) { 42 return document.cookie.split(';'); 43 } 44 45 nameEQ = name + "="; 46 ca = document.cookie.split(';'); 47 for (i = 0; i < ca.length; i += 1) { 48 c = ca[i]; 49 while (c.charAt(0) === ' ') {c = c.substring(1, c.length);} 50 if (c.indexOf(nameEQ) === 0) { 51 return c.substring(nameEQ.length, c.length); 52 } 53 } 54 return null; 55 } 56 57 /** 58 * @description Remove the specified cookie by setting the expiry date to one day ago 59 * @param {String} name The name of the cookie to remove. 60 */ 61 function remove(name) { 62 set(name, "", -1); 63 } 64 65 return { 66 set : set, 67 get : get, 68 remove : remove 69 }; 70 }()); 71 72 73 $$.module('Sfdc.canvas.cookies', module); 74 75 }(Sfdc.canvas));