49
Do
dotter / files
2.6 KB
1 2// usage: log('inside coolFunc', this, arguments); 3// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/ 4window.log = function(){ 5 log.history = log.history || []; // store logs to an array for reference 6 log.history.push(arguments); 7 if(this.console) console.log( Array.prototype.slice.call(arguments) ); 8}; 9 10 11 12// place any jQuery/helper plugins in here, instead of separate, slower script files. 13if (typeof window.localStorage == 'undefined' || typeof window.sessionStorage == 'undefined') (function () { 14 15var Storage = function (type) { 16 function createCookie(name, value, days) { 17 var date, expires; 18 19 if (days) { 20 date = new Date(); 21 date.setTime(date.getTime()+(days*24*60*60*1000)); 22 expires = "; expires="+date.toGMTString(); 23 } else { 24 expires = ""; 25 } 26 document.cookie = name+"="+value+expires+"; path=/"; 27 } 28 29 function readCookie(name) { 30 var nameEQ = name + "=", 31 ca = document.cookie.split(';'), 32 i, c; 33 34 for (i=0; i < ca.length; i++) { 35 c = ca[i]; 36 while (c.charAt(0)==' ') { 37 c = c.substring(1,c.length); 38 } 39 40 if (c.indexOf(nameEQ) == 0) { 41 return c.substring(nameEQ.length,c.length); 42 } 43 } 44 return null; 45 } 46 47 function setData(data) { 48 data = JSON.stringify(data); 49 if (type == 'session') { 50 window.name = data; 51 } else { 52 createCookie('localStorage', data, 365); 53 } 54 } 55 56 function clearData() { 57 if (type == 'session') { 58 window.name = ''; 59 } else { 60 createCookie('localStorage', '', 365); 61 } 62 } 63 64 function getData() { 65 var data = type == 'session' ? window.name : readCookie('localStorage'); 66 return data ? JSON.parse(data) : {}; 67 } 68 69 70 // initialise if there's already data 71 var data = getData(); 72 73 return { 74 length: 0, 75 clear: function () { 76 data = {}; 77 this.length = 0; 78 clearData(); 79 }, 80 getItem: function (key) { 81 return data[key] === undefined ? null : data[key]; 82 }, 83 key: function (i) { 84 // not perfect, but works 85 var ctr = 0; 86 for (var k in data) { 87 if (ctr == i) return k; 88 else ctr++; 89 } 90 return null; 91 }, 92 removeItem: function (key) { 93 delete data[key]; 94 this.length--; 95 setData(data); 96 }, 97 setItem: function (key, value) { 98 data[key] = value+''; // forces the value to a string 99 this.length++; 100 setData(data); 101 } 102 }; 103}; 104 105if (typeof window.localStorage == 'undefined') window.localStorage = new Storage('local'); 106if (typeof window.sessionStorage == 'undefined') window.sessionStorage = new Storage('session'); 107 108})(); 109