Skip to main content

Bug Tracker

Side navigation

#11054 closed bug (invalid)

Opened December 18, 2011 03:17PM UTC

Closed January 11, 2012 04:19PM UTC

Last modified May 27, 2013 09:54PM UTC

Problem with .focus() in IE8.

Reported by: Yoeri Roels <yoeri.roels@gmail.com> Owned by: Yoeri Roels <yoeri.roels@gmail.com>
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.1
Keywords: Cc:
Blocked by: Blocking:
Description

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)

Timestamp: Sun, 18 Dec 2011 15:06:22 UTC

Message: Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus.

Line: 3

Char: 3117

Code: 0

URI: .../jquery-1.7.1.min.js

More info:

http://stackoverflow.com/questions/5675396/javascript-focus-issue-in-ie-for-dynamic-content

I don't know if this is expected behavior, but it only seems to occur in IE8 and possibly older versions. IE9 seems okay.

Attachments (0)
Change History (9)

Changed December 18, 2011 03:27PM UTC by dmethvin comment:1

owner: → Yoeri Roels <yoeri.roels@gmail.com>
status: newpending

I have the same questions as in the SO thread. Why focus an element that isn't visible? IE6/7/8 won't do it and the best you can do is trap the error, in which case the focus won't occur.

If you just need to run the corresponding focus event handlers, can you use .triggerHandler("focus") instead? That won't try to focus the element.

Changed December 19, 2011 10:04PM UTC by Charlie Schams <charlie@embtv.com> comment:2

This is a change in behavior from jQuery 1.6.4 to 1.7 and has bug #1486 referenced in the source near where the error occurs. jQuery 1.7 misses elements that have the CSS property visibility:hidden and are either invalid focus targets or disabled.

Here is an example http://jsfiddle.net/c6evQ/11/.

Changed December 20, 2011 02:44PM UTC by Yoeri Roels <yoeri.roels@gmail.com> comment:3

status: pendingnew

Replying to [comment:1 dmethvin]:

I have the same questions as in the SO thread. Why focus an element that isn't visible? IE6/7/8 won't do it and the best you can do is trap the error, in which case the focus won't occur. If you just need to run the corresponding focus event handlers, can you use .triggerHandler("focus") instead? That won't try to focus the element.

I'm not focusing the element myself, I guess some component I use causes this. Anyway, the fact that behavior is inconsistent (IE vs others) seems like a bug to me.

For my application, I can work around it, but though it was useful reporting it anyway.

Changed January 11, 2012 04:19PM UTC by dmethvin comment:4

resolution: → invalid
status: newclosed

In 1.6.x we trapped and discarded *any* errors thrown by an inline event handler. That is not good because it masks problems that the handler may have. We are trying to remove any of these omnibus try/catch situations in the jQuery code base to prevent that. So this error was occurring before, but just masked.

I think IE8 is justified in being upset that you're trying to set focus to an element the user cannot see or interact with. Most other browsers seem to ignore it since it's not a valid request, but there's no W3C guideline that I know of for what to do when someone asks to focus an invisible element. An error is probably better than silence when it comes to debugging.

If this is being caused by a plugin then the plugin should be fixed. Solutions include showing the element before setting focus to it, or using .triggerHandler() if you just want to run handlers and not actually set focus.

Changed January 24, 2012 09:22AM UTC by anonymous comment:5

Replying to [comment:4 dmethvin]:

In 1.6.x we trapped and discarded *any* errors thrown by an inline event handler. That is not good because it masks problems that the handler may have. We are trying to remove any of these omnibus try/catch situations in the jQuery code base to prevent that. So this error was occurring before, but just masked. I think IE8 is justified in being upset that you're trying to set focus to an element the user cannot see or interact with. Most other browsers seem to ignore it since it's not a valid request, but there's no W3C guideline that I know of for what to do when someone asks to focus an invisible element. An error is probably better than silence when it comes to debugging. If this is being caused by a plugin then the plugin should be fixed. Solutions include showing the element before setting focus to it, or using .triggerHandler() if you just want to run handlers and not actually set focus.

This resolution seems a bit strange to me, but perhaps I don't quite understand jQuery goal.

From where I stand - jQuery is suppose to provide consistent behaviour across browsers and this is not it.

Just in case, though, this is the JSFiddle: http://jsfiddle.net/FrKyN/222/

Changed January 24, 2012 02:26PM UTC by dmethvin comment:6

jQuery provides consistent behavior for a valid set of inputs and actions. Trying to focus an invisible element isn't in that set. Making things consistent across browsers shouldn't mean making all browsers hide an mistake if any of them hide the mistake.

Changed May 07, 2013 04:56PM UTC by lrekucki+jquery@gmail.com comment:7

Replying to [comment:4 dmethvin]:

I think IE8 is justified in being upset that you're trying to set focus to an element the user cannot see or interact with. Most other browsers seem to ignore it since it's not a valid request, but there's no W3C guideline that I know of for what to do when someone asks to focus an invisible element. An error is probably better than silence when it comes to debugging.

Here you go: http://www.w3.org/TR/html5/editing.html#focusing-steps

1. If the element is not in a Document, or if the element's Document has no browsing context, or if the element's Document's browsing context has no top-level browsing context, or if the element is not focusable, then abort these steps.

I am really amazed that someone thought throwing an exception would be better for the user. The fact that it was changed in IE9 should be a clear indication that it's the right thing to do and jQuery should do it!

Changed May 07, 2013 05:04PM UTC by dmethvin comment:8

We don't throw the error, IE8 does. To restate the information above, if we catch errors at this point in the code, we will silence *all* errors including developer logic errors. Even if we attempt to rethrow all but the focus issue, we will prevent important information (line number, file name) from reaching the developer. That in itself would be inconsistent. There is no way to provide a consistent behavior across all the conditions here.

Changed May 27, 2013 09:54PM UTC by anonymous comment:9

This works on IE8!!

$('#dialog').find('#name').blur();

$('#dialog').find('#name').focus();

source: http://stackoverflow.com/questions/1326993/jquery-focus-sometimes-not-working-in-ie8