六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 1697|回复: 0

js判断浏览器Cookie功能是否开启兼容IE,firefox,chrome,opera

[复制链接]
 楼主| 发表于 2014-12-24 17:55:57 | 显示全部楼层 |阅读模式
js判断浏览器Cookie功能是否开启兼容IE,firefox,chrome,opera
jsvascript代码如下:
  1. <script type="text/javascript">

  2.       function CookieEnable()
  3.       {
  4.         var result=false;
  5.         if(navigator.cookiesEnabled)
  6.           return true;
  7.         document.cookie = "testcookie=yes;";
  8.         var cookieSet = document.cookie;
  9.         if (cookieSet.indexOf("testcookie=yes") > -1)
  10.           result=true;
  11.         document.cookie = "";
  12.         return result;
  13.       }

  14.       if(!CookieEnable())
  15.       {
  16.         alert("对不起,您的浏览器的Cookie功能被禁用,请开启");
  17.       }
  18. </script>
复制代码
说明: 上面的js代码主要是 写一个cookie到客户端然后马上再来读取这个cookie,如果读取到的cookie为空,
则表示客户端禁止了cookie,那我们就不要再用cookie来储存用户信息了

兼容IE,firefox,chrome,opera的COOKIE检测试代码为:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">

  3. <head>
  4. <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
  5. <title>您的浏览器禁止了脚本运行</title>
  6. <script language="javascript" type="text/javascript">
  7. window.onload=function()
  8. {
  9. document.getElementById('jscheck').style.display='none';
  10. document.getElementById('normalcontent').style.display='';
  11. if(!(document.cookie || navigator.cookieEnabled))
  12. {
  13. alert('cookie 未打开!');
  14. }
  15. };
  16. </script>
  17. </head>

  18. <body>
  19. <div id="jscheck">您的浏览器禁止了脚本运行,本系统无法启动!</div>
  20. <div id="normalcontent" style="display:none;">这里是正文</div>
  21. </body>

  22. </html>
复制代码
js判断浏览器Cookie功能是否开启兼容IE,firefox,chrome,opera

参考:
http://blog.sina.com.cn/s/blog_6436b8ec0100y7up.html
http://www.cnblogs.com/Ren_Lei/archive/2010/09/30/1839399.html
该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表