123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- /* *
- * LsyCookie
- * author loushengyue
- * website http://www.loushengyue.com
- * version 1.2.4
- * methods [setItem(),getItem(),getAll(),removeItem(),,clear()]
- */
- ;(function (win, doc) {
- var LsyCookie, expiresTime = 24 * 3600, version = '1.2.4';
- LsyCookie = function () {
- this.version = 'LsyCookie ' + version;
- };
- /* *
- * create cookie by key and value, and set expires time
- * key typeof string
- * val typeof string
- * path typeof string
- * time typeof number[7days]
- */
- LsyCookie.prototype.setItem = function (key, val, time, path) {
- var exp, pathStr = '';
- checkStr(key);
- val = resetVal(val);
- time = resetTime(time);
- exp = new Date();
- exp.setTime(exp.getTime() + time * 1000);
- if (path) {
- checkStr(path);
- path = resetPath(path);
- pathStr = path ? ';path=' + path : pathStr;
- }
- doc.cookie = key + '=' + val + ';expires=' + exp.toGMTString() + pathStr;
- };
- /* *
- * remove cookie by key
- * key typeof string
- */
- LsyCookie.prototype.removeItem = function (key, path) {
- path = path ? path : './';
- this.set(key, '', -1, path);
- };
- /* *
- * remove all cookies
- */
- LsyCookie.prototype.clear = function () {
- var _this = this;
- var keys = Object.keys(_this.getAll());
- keys.forEach(function (item) {
- _this.removeItem(item);
- });
- };
- /* *
- * get all cookies, and return object
- */
- LsyCookie.prototype.getAll = function () {
- var cookies = doc.cookie.split(/;\s/);
- var cookieObj = {};
- cookies.forEach(function (item) {
- var key = item.split('=')[0];
- var val = item.split('=')[1];
- val = resetJsonStr(val);
- cookieObj[key] = val;
- });
- return cookieObj;
- };
- /* *
- * get cookie by key
- * key typeof string
- */
- LsyCookie.prototype.getItem = function (key) {
- return this.getAll()[key];
- };
- /* *
- * reset expires time
- * time typeof number[7days]
- */
- function resetTime(time) {
- if (time != null) {
- if (isNaN(time)) {
- throw new Error('The typeof time argument in resetTime(time){...} must be number. But the typeof your argument "' + time + '" is ' + typeof time);
- }
- time = time > 0 ? time : -1;
- } else {
- time = expiresTime;
- }
- return time;
- }
- /* *
- * ckeck val is the right typeof string, if not, change it.
- * val typeof String,Array,Object
- * return typeof string
- */
- function resetVal(val) {
- if (typeof val === 'object') {
- val = JSON.stringify(val);
- }
- return val;
- }
- /* *
- * ckeck key is the right typeof string, if not, change it.
- * val typeof String,Array,Object
- * return typeof string
- */
- function checkStr(key) {
- if (typeof key != 'string') {
- throw new Error('The typeof str argument in setItem(key[string],value[string|object]){...} must be string. But the typeof your argument "' + key + '" is ' + typeof key);
- }
- }
- /* *
- * reset string for json.
- * return typeof String,Array,Object
- */
- function resetJsonStr(str) {
- var obj;
- try {
- obj = JSON.parse(str);
- return obj;
- } catch (e) {
- return str;
- }
- }
- /* *
- * reset path
- * path typeof string
- */
- function resetPath(path) {
- if (/^(\.)?\.\//.test(path)) {
- var timesArr, n,
- index = -1,
- pathName = location.pathname;
- path = path.replace(/^\.\//, '');
- timesArr = path.match(/\.\.\//g) || [];
- n = timesArr.length + 1;
- for (; n--;) {
- index = pathName.lastIndexOf('/');
- if (index != -1) {
- pathName = pathName.substr(0, index);
- }
- }
- path = path.replace(/\.\.\//g, '');
- return pathName + '/' + path;
- }
- return path;
- }
- win['LsyCookie'] = new LsyCookie();
- })(window, document);
|