Bug Tracker

Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#11054 closed bug (invalid)

Problem with .focus() in IE8.

Reported by: Yoeri Roels <yoeri.roels@…> Owned by: Yoeri Roels <yoeri.roels@…>
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.

Change History (9)

comment:1 Changed 8 years ago by dmethvin

Owner: set to Yoeri Roels <yoeri.roels@…>
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.

comment:2 Changed 8 years ago by Charlie Schams <charlie@…>

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/.

comment:3 in reply to:  1 Changed 8 years ago by Yoeri Roels <yoeri.roels@…>

Status: pendingnew

Replying to 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.

comment:4 Changed 8 years ago by dmethvin

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.

comment:5 in reply to:  4 Changed 8 years ago by anonymous

Replying to 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/

comment:6 Changed 8 years ago by dmethvin

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.

comment:7 in reply to:  4 Changed 7 years ago by lrekucki+jquery@…

Replying to 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!

comment:8 Changed 7 years ago by dmethvin

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.

comment:9 Changed 7 years ago by anonymous

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

Note: See TracTickets for help on using tickets.