简体   繁体   中英

onunload and onbeforeunlad events not working as expected in IE11 and IIS

I'm working on a web application in VS 2013 on Windows 8.1 that is hosted on IIS 8.5 on a 2012 R2 Server, and using the IE11 browser. I'm trying to use the unload event to send information to a web service to indicate that a user is no longer using a form that was unloaded so that others can use it (sort of a licensing scenario). That web service is Third Party, and I can't change that code. The code below is a fragment that shows how the JavaScript is set up for the unload and beforeunload events.

The unload event works/doesn't work as follows:

  1. In VS, while debugging the application both the unload and beforeunload events are working. The beforeunload provides a message box asking the user if he wants to exit, and the unload event successfully calls the web service.

  2. When IE11 is accesses the app on IIS with the F12 developer tools open, I get the same result. The beforeunload provides a message box asking the user if he wants to exit, and the unload event successfully calls the web service.

  3. When IE11 accesses the app on IIS without the F12 developer tools open, beforeunload works properly, but the unload event does not call the web service (or the browser shuts down before the call to the web service transmits.

  4. If I move the web service call to the beforeunload event, comment out the unload event, and don't provide the beforeunload event with a return statement (hence blocking the message popup), the beforeunload event does not call the web service (or the browser shuts down before the call to the web service transmits?)

I've run through a lot of Googles trying to track this down, but none of the proposed solutions seem to work, or require modifying the third party code.

One suggestion said that it might be related to a Windows Group policy affecting IE11, but I was unable to track that down.

I suspect I'm making a simple mistake here. Any help would be appreciated…

  < script type = "text/javascript" > $(window).on('mouseover', (function() { window.onbeforeunload = null; })); $(window).on('mouseout', (function() { window.onbeforeunload = ConfirmLeave; })); var prevKey = ""; $(document).keydown(function(e) { if (e.key == "F5") { window.onbeforeunload = ConfirmLeave; } else if (e.key.toUpperCase() == "W" && prevKey == "CONTROL") { window.onbeforeunload = ConfirmLeave; } else if (e.key.toUpperCase() == "R" && prevKey == "CONTROL") { window.onbeforeunload = ConfirmLeave; } else if (e.key.toUpperCase() == "F4" && (prevKey == "ALT" || prevKey == "CONTROL")) { window.onbeforeunload = ConfirmLeave; } prevKey = e.key.toUpperCase(); }); function ConfirmLeave() { return "Are you sure you want to exit EBS Quote to Sale?"; } $(window).on('unload', (function() { RemoveActiveUser(); })); function RemoveActiveUser() { var cUsertoken = readCookie('userToken'); var wsUrls = webServiceurl + "?op="; wsUrl = wsUrls + "RemoveActiveUser"; var soapRequest = '<?xml version="1.0" encoding="utf-8"?> \\ <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \\ xmlns:xsd="http://www.w3.org/2001/XMLSchema" \\ xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> \\ <soap:Body> \\ <RemoveActiveUser xmlns="http://portal.ebs-next.com/EbsQuoteToSale/"> \\ <usrToken>' + $.trim(cUsertoken) + '</usrToken> \\ </RemoveActiveUser> \\ </soap:Body> \\ </soap:Envelope>'; $.ajax({ type: "POST", url: wsUrl, contentType: "text/xml", crossDomain: true, data: soapRequest, success: function(data, status, req) { if (status === "success") { $('.error').html(''); $('.error').html(''); var jresult = $(req.responseText).find("RemoveActiveUserResult").text(); var obj = jQuery.parseJSON(jresult); if (obj === '' || obj.length === 0) { $('.error').html(''); $('.error').css('color', '#F00'); $('.error').html(''); } else {} } } }); eraseCookie('userID'); eraseCookie('userName'); eraseCookie('userRoleID'); eraseCookie('userToken'); eraseCookie('userQouteRights'); eraseCookie('userSaleRights'); }.< /script> 

You may use e.keyCode, e.ctrlKey & e.altKey.

$(document).keydown(function(e) {
    if (e.keyCode == 116) {
        window.onbeforeunload = ConfirmLeave;
    } else if (e.keyCode == 87 && e.ctrlKey) {
        window.onbeforeunload = ConfirmLeave;
    } else if (e.keyCode == 82 && e.ctrlKey) {
        window.onbeforeunload = ConfirmLeave;
    } else if (e.keyCode == 115 && (altKey || ctrlKey)) {
        window.onbeforeunload = ConfirmLeave;
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM