onunload和onbeforeunlad事件在IE11和IIS中无法按预期工作 [英] onunload and onbeforeunlad events not working as expected in IE11 and IIS

查看:195
本文介绍了onunload和onbeforeunlad事件在IE11和IIS中无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows 8.1上的VS 2013中的Web应用程序,该应用程序托管在2012 R2服务器上的IIS 8.5上,并使用IE11浏览器.我正在尝试使用unload事件将信息发送到Web服务,以指示用户不再使用已卸载的表单,以便其他人可以使用它(某种许可情况).该Web服务是第三方,我无法更改该代码.下面的代码是一个片段,显示了如何为unload和beforeunload事件设置JavaScript.

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. 在VS中,在调试应用程序时,卸载和 beforeunload事件正在运行. beforeunload提供了一个 消息框,询问用户是否要退出,然后卸载 事件成功调用Web服务.

  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.

使用F12开发人员工具在IE11上访问IIS上的应用程序时 打开,我得到相同的结果. beforeunload提供一个消息框 询问用户是否要退出,以及卸载事件 成功调用Web服务.

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.

当IE11在不使用F12开发人员工具的情况下访问IIS上的应用程序时 打开,beforeunload可以正常工作,但是unload事件不起作用 调用网络服务(或在调用之前关闭浏览器 Web服务传输.

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.

如果将Web服务调用移至beforeunload事件,请注释 退出unload事件,并且不提供带有 返回语句(因此阻止了消息弹出窗口), beforeunload事件不会调用Web服务(或浏览器) 在调用Web服务之前关闭?)

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?)

我遇到了很多Google试图追踪这种情况,但是所提出的解决方案似乎都不起作用,或者需要修改第三方代码.

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.

一个建议说,这可能与影响IE11的Windows组策略有关,但我无法对其进行跟踪.

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>

推荐答案

您可以使用e.keyCode,e.ctrlKey& e.altKey.

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;
    }
});

这篇关于onunload和onbeforeunlad事件在IE11和IIS中无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆