var cookie = function() { return { setCookie: function(name, value, option) { var date = new Date() var expires = option.expires ? option.expires : '' var path = option.path ? '; path=' + option.path : '' var domain = option.domain ? '; domain=' + option.domain : '' var secure = option.secure ? '; secure' : '' date.setTime(date.getTime() + expires * 60 * 1000) document.cookie = name + "=" + escape(value) + ";expires=" + date.toGMTString() + path + domain + secure }, getCookie: function(name) { var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); if (arr != null) { return unescape(arr[2]) } return null }, delCookie: function(name, option) { var date = new Date() var cval = this.getCookie(name) var path = option.path ? '; path=' + option.path : '' var domain = option.domain ? '; domain=' + option.domain : '' var secure = option.secure ? '; secure' : '' date.setTime(date.getTime() - 1000) if (cval != null) { document.cookie = name + "=" + escape(cval) + ";expires=" + date.toGMTString() + path + domain + secure } } } }() cookie.setCookie('date', new Date(), {expires: 1 * 24 * 60, path: '/', domain: '.baomihua.com'}); console.log(cookie.getCookie('date')); setTimeout(function() { cookie.delCookie('date', {path: '/', domain: '.baomihua.com'}); }, 2 * 1000);