javascript – onbeforeUnload没有返回或没有对话框显示
作者:互联网
我正在使用richfaces在java EE中开发一个Web应用程序.
有一个链接,用于注销用户.
当用户离开页面或关闭浏览器时,我应该注销用户,因为我必须知道他何时离开我们的页面.
这是一个片段:
function fireEvent(obj,evt){
var fireOnThis = obj;
if( document.createEvent ) {
var evObj = document.createEvent('MouseEvents');
evObj.initEvent( evt, true, false );
fireOnThis.dispatchEvent(evObj);
} else if( document.createEventObject ) {
fireOnThis.fireEvent('on'+evt);
}
}
var popit = true;
window.onbeforeunload = function() {
if(popit == true) {
fireEvent(document.getElementById("logout_form:logout_link"),'click');
}
}
这仅适用于浏览器关闭,不能导航或刷新.
我改变了fireEvent(…)以返回“”.一切都很好.
如果我在fireEvent(…)之后键入return“”它运行良好,只是我不想显示对话框.
我怎么能这样做?
解决方法:
使用synchronous AJAX调用,然后将关闭方法放入回调中:
window.onbeforeunload = function() {
// This template uses no error checking, it's just concept code to be
// expanded on.
// Create a new XMLHttpRequest object
var AJAX=new XMLHttpRequest();
// Handle ready state changes ( ignore them until readyState = 4 )
AJAX.onreadystatechange= function() { if (AJAX.readyState!=4) return false; }
// we're passing false so this is a synchronous request.
// The script will stall until the document has been loaded.
// the open statement depends on a global variable titled _userID.
AJAX.open("GET", 'about:blank', false);
AJAX.send(null);
}
标签:javascript,browser,java-ee,richfaces 来源: https://codeday.me/bug/20190709/1412702.html