admin管理员组

文章数量:1323729

This code works fine in <= IE7, but it doesn't work in firefox .. i am using firefox browser 12.0 .. I am not sure whats the reason .. help would be much appreciated .. thanks ..

<head>
 <script type="text/javascript">
   function getfocus(obj){
      if(obj.value.length==0){
        alert("Please enter something");
        obj.focus();
      }
   }
 </script>
</head>

<body>
   <input type="text" onblur="getfocus(this)" value="Get focus">
</body>

</html>

This code works fine in <= IE7, but it doesn't work in firefox .. i am using firefox browser 12.0 .. I am not sure whats the reason .. help would be much appreciated .. thanks ..

<head>
 <script type="text/javascript">
   function getfocus(obj){
      if(obj.value.length==0){
        alert("Please enter something");
        obj.focus();
      }
   }
 </script>
</head>

<body>
   <input type="text" onblur="getfocus(this)" value="Get focus">
</body>

</html>
Share Improve this question edited May 26, 2012 at 12:39 Sanath 4,88611 gold badges57 silver badges87 bronze badges asked May 25, 2012 at 11:30 Sakthivel ViswanathanSakthivel Viswanathan 832 silver badges6 bronze badges 2
  • what exactly happens on Firefox? What are you expecting? – Fabrizio Calderan Commented May 25, 2012 at 11:34
  • What part isn't working? Where does it break/stop? Any errors in your JavaScript console? – David Thomas Commented May 25, 2012 at 11:34
Add a ment  | 

3 Answers 3

Reset to default 5

try:

....
alert("Please enter something");
setTimeout(function() {
    obj.focus()
}, 10);

Some browsers, notably Firefox, have user-specific settings which control whether focus may be 'stolen' from the user. I think this may be your problem here.

However, you might want to re-consider your tactics here for ensuring the user enters some text. Having an alert pop-up every time you unfocus a blank text field would be extremely irritating to most users.

The code is working fine to me both on FF 12 and Chrome. You need to clear the input filed and then get out of the item in order to see the alert...

本文标签: htmlJavascript focus not working in firefoxStack Overflow