Instantly share code, notes, and snippets.
Created
August 10, 2016 09:20
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(1)
1
You must be signed in to fork a gist
-
Save anonymous/433a04b7f9f84ef2f81257c52e4531ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var docCookies = { | |
getItem: function (sKey) { | |
if (!sKey) { return null; } | |
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null; | |
}, | |
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) { | |
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; } | |
var sExpires = ""; | |
if (vEnd) { | |
switch (vEnd.constructor) { | |
case Number: | |
sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd; | |
break; | |
case String: | |
sExpires = "; expires=" + vEnd; | |
break; | |
case Date: | |
sExpires = "; expires=" + vEnd.toUTCString(); | |
break; | |
} | |
} | |
document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : ""); | |
return true; | |
}, | |
removeItem: function (sKey, sPath, sDomain) { | |
if (!this.hasItem(sKey)) { return false; } | |
document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : ""); | |
return true; | |
}, | |
hasItem: function (sKey) { | |
if (!sKey) { return false; } | |
return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); | |
}, | |
keys: function () { | |
var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/); | |
for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); } | |
return aKeys; | |
} | |
}; | |
;(function(){ | |
var xbm = window.xbm || {}; | |
window.xbm = xbm; | |
/** | |
* 小斑马 localStorage 辅助方法 | |
* @author github.com/lgh06 | |
* @date 2016-03-09T10:54:13+0800 | |
* @param {String} key [键名] | |
* @param {String} item [值] | |
*/ | |
xbm.set = function (key,item){ | |
window.localStorage ? window.localStorage.setItem(key,JSON.stringify(item)) : 0; | |
} | |
xbm.get = function (key){ | |
return window.localStorage ? JSON.parse( window.localStorage.getItem(key) ) : ''; | |
} | |
xbm.del = function (key){ | |
window.localStorage ? window.localStorage.removeItem(key) : 0; | |
} | |
xbm.clear = function(not){ | |
if(typeof not === 'string' || typeof not === 'undefined'){ | |
window.localStorage | |
? | |
Object.keys(window.localStorage).forEach(function(v,i,a){ | |
if( v !== not){ | |
xbm.del(v); | |
} | |
}) | |
: | |
0 ; | |
} | |
if( typeof not == 'object' && not.length > 0 ){ | |
var localArr = Object.keys(window.localStorage); | |
localArr.forEach(function(v,i,a){ | |
not.forEach(function(vv,ii,aa){ | |
if(vv == v){ | |
delete localArr[i] | |
} | |
}); | |
}) | |
localArr.forEach(function(v,i,a){ | |
xbm.del(v); | |
}); | |
} | |
} | |
xbm.getCookie = function (key){ | |
var length = key.length;//键名长度 | |
var fromIndex = document.cookie.indexOf(key+'=');//起始index | |
var endIndex = document.cookie.indexOf(';',fromIndex);//结束index | |
if(endIndex === -1) {endIndex = document.cookie.length;}//最后的cookie可能没有分号 | |
var str = document.cookie.substring(fromIndex,endIndex); | |
return str.substring(length+1,str.length);//去除xxx= 只留下值 | |
} | |
//显示一个消息,会在2秒钟后自动消失 | |
xbm.toast = function(msg, duration, extraclass,callback) { | |
var $toast = $('<div class="modal toast ' + (extraclass || '') + '">' + msg + '</div>').appendTo(document.body); | |
$.openModal($toast, function(){ | |
setTimeout(function() { | |
$.closeModal($toast); | |
if(typeof callback !== 'undefined'){ | |
callback(); | |
} | |
}, duration || 2000); | |
}); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment