由于各浏览器兼容性不同,所以首先要先区分各浏览器
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器
var isIE11 = userAgent.indexOf("rv:11.0") > -1; //判断是否是IE11浏览器
var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
if(!isIE && !isEdge && !isIE11) {//兼容chrome和firefox
var _beforeUnload_time = 0, _gap_time = 0;
var is_fireFox = navigator.userAgent.indexOf("Firefox")>-1;//是否是火狐浏览器
window.onunload = function (){
_gap_time = new Date().getTime() - _beforeUnload_time;
if(_gap_time <= 5){
$.post('webLoginController.do?delSession2');//浏览器关闭
}else{//浏览器刷新
}
}
window.onbeforeunload = function (){
_beforeUnload_time = new Date().getTime();
if(is_fireFox){//火狐关闭执行
$.post('webLoginController.do?delSession2');//浏览器关闭
}
};
}
if(isIE) {//兼容ie8,9,10
window.onbeforeunload = function() {
//alert(0);
flagiii = "1";
$.ajax({
type: "post",
url: "webLoginController.do?A",
data:{"flagiii": flagiii},
success: function() {
},
async:false
});
}
window.onunload = onclose;
function onclose(){
flagiii = "0";
$.ajax({
type: "post",
url: "webLoginController.do?storageData",
data:{"flagiii": flagiii},
success: function() {
}
});
}
}
/**
* 关闭浏览器时清除session兼容IE9和IE10浏览器
* 让方法延迟10s执行
* create by lucy
* 2017-12-19
*/
@RequestMapping(params = "A")
public void A(
@RequestParam("flagiii") String flagiii,
final HttpSession session,
final HttpServletRequest req
) {
session.setAttribute("flagiii", flagiii);
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
System.out.println("begin");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end");
delData(session, req);
}
});
thread.start();
}
public void B(HttpSession session,HttpServletRequest req) {
String flagiii = (String) session.getAttribute("flagiii");
if("1".equals(flagiii)) {
boolean isTrue = false;
String userId = session.getAttribute("userId").toString();
isTrue = webLoginService.deleteOnline(userId);
//将安全日志插入数据库 map
SafeLogInsertDBUtils.safeLogInsertDB(req, session, "关闭浏览器", true,StringUtil.LOGINOUT);
}
}
/**
* 存储标记(配合delSessionData方法)
* @param flagiii
* create by lucy
* 2017-12-19
*/
@RequestMapping(params = "B")
public void B(
HttpSession session,
@RequestParam("flagiii") String flagiii
) {
session.setAttribute("flagiii", flagiii);
}
var flagiii = "0";
function onloadFun() {
$.ajax({
type: "post",
url: "webLoginController.do?storageData",
data:{"flagiii": flagiii},
success: function() {
}
});
}
if(isIE || isIE11 || isEdge) {
//页面加载进来后将flagii存入session
onloadFun();
}
if(isIE11 || isEdge) {//兼容ie11 edge
window.onbeforeunload = function() {
flagiii = "1";
$.ajax({
type: "post",
url: "webLoginController.do?storageData",
data:{"flagiii": flagiii},
success: function() {
},
async:false
});
}
window.onunload = onclose;
function onclose(){
$.ajax({
type: "post",
url: "webLoginController.do?delSessionData",
data: {"rnd": Math.random()},
success: function() {
}
});
}
}
/**
* 存储标记(配合delSessionData方法)
* @param flagiii
* create by lucy
* 2017-12-19
*/
@RequestMapping(params = "storageData")
@ResponseBody
public void storageData(
HttpSession session,
@RequestParam("flagiii") String flagiii) {
session.setAttribute("flagiii", flagiii);
}
/**
* 关闭浏览器时清除session兼容IE11和Edge浏览器
* 让方法延迟30s执行
* create by lucy
* 2017-12-19
*/
@RequestMapping(params = "delSessionData")
@ResponseBody
public void delSessionData(final HttpSession session, final HttpServletRequest req) {
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
System.out.println("begin");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end");
delData(session, req);
}
});
thread.start();
}