Chinaunix首页 | 论坛 | 博客
  • 博客访问: 164219
  • 博文数量: 43
  • 博客积分: 95
  • 博客等级: 民兵
  • 技术积分: 215
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-24 17:23
文章分类

全部博文(43)

文章存档

2016年(1)

2015年(5)

2014年(10)

2013年(24)

2012年(3)

我的朋友

分类: JavaScript

2013-12-25 16:47:02

一下代码完全兼容IE、Firefox、Chrome浏览器,实现Authorization:Basic 密码验证的注销 logout。至于其他浏览器理论上可以但没有测试。
注销原理是像服务器发送错误验证信息,对服务器进行读写操作,使浏览器判断当前用户是非法用户,要重新登录。
最近项目要同时兼容三个浏览器:IE、firefox、chrome
登陆框密码是浏览器自动产生,在注销时发生各种问题,IE能很好的logout
firefox、chrome浏览器能记住密码,很难实现logout,网上说故意发送一个错误的密码就OK了,由于现代浏览器都很智能,第一次能logout,再次登录后就不能成功logout了
我用随机产生的用户名密码可以解决以上问题,源码如下:

点击(此处)折叠或打开

  1. function createXMLObject() {
  2.     var xmlHttp;
  3.     try {
  4.         if (window.XMLHttpRequest) {
  5.             xmlHttp = new XMLHttpRequest();
  6.         }
  7.         else if (window.ActiveXObject) {
  8.             xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  9.         }
  10.     } catch (e) {
  11.         xmlHttp=false
  12.     }
  13.     return xmlHttp;
  14. }
  15. function clearAuthenticationCache(page) {
  16.     if (!page) page = '/';
  17.     
  18.     try {
  19.         var agt=navigator.userAgent.toLowerCase();
  20.         
  21.         if (agt.indexOf("msie") != -1 ||agt.indexOf("trident") != -1) {
  22.             // IE clear HTTP Authentication
  23.             document.execCommand("ClearAuthenticationCache");
  24.         }
  25.         else {
  26.             var logout=String(Math.random()*65536 + 1);
  27.             var xmlhttp = createXMLObject();
  28.             xmlhttp.open("POST", page, false,logout,base64encode(logout));
  29.             xmlhttp.send(null);
  30.         }
  31.     } catch(e) {
  32.         warn_logout_fail();
  33.         window.location.href="wwwctrl.cgi?action=DEFAULT";
  34.         return;
  35.     }
  36.     return;
  37. }
  38. clearAuthenticationCache();
  39. window.location.href='/';

阅读(7859) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~