简体   繁体   中英

window.onblur event also triggers window.onfocus event when using IE8

Both the events are getting fired at the time onblur event.

<%@ Page Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"  CodeBehind="WebForm1.aspx.cs"
Inherits="WebApplication1.WebForm1" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script src="js/jquery-1.7.2.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        window.onfocus = function() {
            $('#msg').html($('#msg').html() + '<br/> focus');
        };
        window.onblur = function() {
            $('#msg').html($('#msg').html() + '<br/> Blur');
        };
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">   
    <div id="msg">
    </div>
</asp:Content>

Finally it got solved. Thank you for all the help!

$(window).bind("load", function() {
        pageLoad();
    });

    var activeElement;

    function pageLoad() {
        activeElement = document.activeElement;
        document.onfocusout = logWindow;
    }

    function logWindow() {
        if (activeElement != document.activeElement) {
            activeElement = document.activeElement;
        }
        else {
            $('#msg').html($('#msg').html()+'<br/> Focus');
        }

    }
 window.onblur = function() { 
      $('#msg').html($('#msg').html() + '<br/> Blur');     
      return false;
 }; 

May be the event is getting bubbled up.

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