检查cookie是否在浏览器中启用?

时间:2014-12-09 13:27:59

标签: javascript jquery

我已在IE中禁用了Cookie,并希望在jquery的帮助下检查是否在浏览器中启用了Cookie。我已经编写了一些要检查的功能,但它在IE浏览器中无效。

在IE中禁用cookie后。我想在IE broswer中设置cookie。即使我在设置中禁用了cookie,它仍在设置。

提前致谢

代码:

 $.cookie = function (name, value, options) {
 if (typeof value != 'undefined') { // name and value given, set cookie
   options = options ||{};
 if (value === null) {
  value = '';
  options.expires = -1;
 }
  var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
  var date;
if (typeof options.expires == 'number') {
 date = new Date();
 date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
 } else {
   date = options.expires;
}
   expires = '; expires=' + date.toUTCString(); }                 
var path = options.path ? '; path=' + (options.path) : '';
 var domain = options.domain ? '; domain=' + (options.domain) : '';
  var secure = options.secure ? '; secure' : '';
  document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
 } else { // only name given, get cookie
 var cookieValue = null;
 if (document.cookie && document.cookie != '') {
 var cookies = document.cookie.split(';');
 for (var i = 0; i < cookies.length; i++) {
 var cookie = $.trim(cookies[i]);

 if (cookie.substring(0, name.length + 1) == (name + '=')) {
  cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
 break;
 }}
 }
 return cookieValue;
 }
  };
 $.cookie("locations", 1);
 if ($.cookie('locations') == null) {
 //cookie does not exist, just set cookie to empty
  alert('cookie empty');       
 }

0 个答案:

没有答案