<div class="gmail_quote">On Thu, Jul 29, 2010 at 2:53 PM, Ian Hickson <span dir="ltr"><<a href="mailto:ian@hixie.ch">ian@hixie.ch</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

On Fri, 16 Apr 2010, Kit Grose wrote:<br>
><br>
> I agree with you both generally, but I disagree that there are no<br>
> downsides. I imagine the main use-case where this sort of behaviour<br>
> might be expected is a Javascript application which dynamically adds a<br>
> new form to the page based on some user interaction and one of those<br>
> fields should be autofocused when it's been added to the DOM.<br>
<br>
I don't think that's the main use case. The main use case is static pages<br>
that just use script to focus the control.<br>
<br>
However, I could see an argument that we should handle the case of a page<br>
that loads a stub script which then loads a blob of HTML in and inserts it<br>
after onload -- that, in combination with the points above, argues that we<br>
should not prevent autofocus from working after onload, but that we should<br>
make it only work once. I've specced that.<br>
<br>
<br>
> For instance, picture the Gmail model. When you first load, the<br>
> autofocus attribute could conceivably be on the inbox search field. When<br>
> you click Compose, the compose form will be dynamically added and the<br>
> "To" field should be autofocused (but the search field is still<br>
> on-screen)<br>
><br>
> I suppose you could argue that it'd be up to the application to go and<br>
> *remove* the previous autofocus attribute from the search field, or<br>
> should manage focus as they currently do with HTMLInputElement.focus(),<br>
> but I can see the simplicity that automatically moving the focus would<br>
> represent for these sorts of applications.<br>
<br>
In this kind of situation, you'd just use focus(). There's not much point<br>
using autofocus if you're already running code; the main win of the<br>
attribute is simplifying the page and not requiring scripting, but if<br>
you've already got code the cost of an additional focus() is minimal.<br></blockquote></div><br>I agree that if you are comparing:<br><br>var html = "<input id='mySearch' type='text' autofocus>";<br>


document.getElementById('myDiv').innerHTML = html;<br><br>to<br><br>var html = "<input id='mySearch' type='text'>";<br>

document.getElementById('myDiv').innerHTML = html;<br>
document.getElementById('mySearch').focus();<br><br>then the cost is minimal.<br><br>However there are a lot of instances of scripts retrieving HTML blobs where it is much more convenient and compatible with the work flow that the focus is determined using HTML instead of JS. If you take a common example such as a Facebook-style modal "dialog" you can see that it would be easy to return HTML with an <input autofocus>. Any HTML editor/generator could set focus this way and it would be easy to convey that functionality to the UA as it is part of the content.<br>

<br>As soon as it you get into requiring JS to call .focus() for any focus action after onload then things get complicated. It becomes something that has to be conveyed out-of-band from the HTML throughout the whole work flow. Now my HTML form editor needs to tell the backend to tell the UA to call .focus() on a specific element after that HTML has been sent _and_ rendered to the client. That can be done, but it certainly isn't elegant or simple.<br>

<br>It would seem to me that focus should always be set on the last element to have autofocus. This would address the "dialog" scenario and work well with common CMS practices. A site may use a CMS (e.g. a blog, Drupal, SharePoint, etc) that may generate a "static" page (from the perspective of the UA) that has autofocus on a search box at the top of the page. However, when the user goes to a page with a form it would autofocus on the form as it would be farther down the page (presumably).<br>

<br>Paul Ellis<br>