Google chrome causes an infinite loop in the focus of the text field

I get some weird events on Google Chrome. With the following code, I get an infinite number of warnings.

<input type="text" />

 $('input[type="text"]').live('focus', function(event) { alert('in'); }); 

http://jsfiddle.net/XppG9/

Firefox and IE8 are both good.

Why is this happening in chrome?

+6
source share
5 answers

I think this is because after closing the dialog box (warning window), the focus returns to the text box, so the function will light up again.

+9
source

I think because the browser sends focus from the warning to the text box every time you click the OK button. You probably wonโ€™t issue a warning (methinks) in the final version of your code, so this may not be the problem in the end.

+5
source

The problem is that alert() steals focus from the input field and then restores it when the dialog is closed. You can fix this by clearing focus from the input window before displaying a warning.

Example: http://jsfiddle.net/XppG9/6/

+2
source

This is because it sets focus back to the text box. Try this, it should work fine in Chrome

 $('input[type="text"]').live('focus', function(event) { alert('in'); $(this).blur(); }); 
+1
source

Because the alert receives focus from your text box, and when you close the alert dialog, the focus returns. If your function has any non-focusing mechanism, it will be launched only once: http://jsfiddle.net/G8CmV/

 <input type="text" /> <div id='tester'>Test:</div> $('input[type="text"]').live('focus', function(event) { $('#tester').html( $('#tester').html() + "_*" ); }); 
0
source

Source: https://habr.com/ru/post/893736/


All Articles