简体   繁体   中英

IE Onblur and focus issue

I am trying to show alert and focus on control when user try to leave control without entering any value in the control. This requirement is something like user is forced to enter values (I know there are certain limitations of such requirements).

When user leaves textbox1 alert is shown and at the sametime alert for textbox2 is also displayed as I am trying to focus on textbox1. This becomes infinite loop in IE and both the pop up keep on displaying in IE.

This code works perfectly in chrome but not in any version of ie.

Code sniphet below:

<html>
    <head>
        <script language="javascript">
        function ShowAlertAndFocus1(){
            var txt1 = document.getElementById("txtBox1");
            if(txt1.value.length == 0){
                alert("Blur 1 called");
                txt1.focus();
            };
        };

        function ShowAlertAndFocus2(){
            var txt2 = document.getElementById("txtBox2");
            if(txt2.value.length == 0){
                alert("Blur 2 called");
                txt2.focus();
            };
        };
        </script>
    </head>

    <body> 
        <input type="text" id = "txtBox1" onblur="ShowAlertAndFocus1();"/>
        <input type="text" id = "txtBox2" onblur="ShowAlertAndFocus2();"/>
    </body>
</html>

I am not sure if am missing something or this limitation is with IE only?

<!DOCTYPE html>
<html>
    <head>
        <title> x </title>
        <script>
            function setOnBlur( txtBox,n ){
                setTimeout( function(){
                                if (document.activeElement==txtBox) {
                                    txtBox.onblur=function(){
                                        if (txtBox.value.length == 0){
                                            alert("Blur "+n+" called")
                                            setTimeout( function(){txtBox.focus()},0 )
                                        }
                                        else txtBox.onblur=null
                                    }
                                }
                            },0)
            }
        </script>
    </head>

    <body> 
        <input type=text id=txtBox1 onfocus=setOnBlur(txtBox1,1) >
        <input type=text id=txtBox2 onfocus=setOnBlur(txtBox2,2) >
    </body>
</html>

No Proper Solutions found till now.

EDIT -

Trick - I used two variables and set them inside the methods. I again checked the values before showing the popup.

Basically, your alerts cause the focus to go away as soon as you focus on the text fields. It is an odd behavior in IE that the blur event comes first. Maybe you can try replacing the alerts and try using console.log instead (That would only work in IE 8 & 9 if developer tools are opened). Or best, you can remove the alerts completely. That should work.

<html>
    <head>
        <script language="javascript">
        function ShowAlertAndFocus1(){
            var txt1 = document.getElementById("txtBox1");
            if(txt1.value.length == 0){
                console.log("Blur 1 called");
                txt1.focus();
            };
        };

        function ShowAlertAndFocus2(){
            var txt2 = document.getElementById("txtBox2");
            if(txt2.value.length == 0){
                console.log("Blur 2 called");
                txt2.focus();
            };
        };
        </script>
    </head>

    <body> 
        <input type="text" id = "txtBox1" onblur="ShowAlertAndFocus1();"/>
        <input type="text" id = "txtBox2" onblur="ShowAlertAndFocus2();"/>
    </body>
</html>

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