0

On iOS 6.0 Safari, it is not possible to assign (hardware, on-screen) keyboard focus to an input element with JavaScript, except perhaps when the current script is running in response to a user action.

How do I detect whether it is possible to set keyboard focus via JavaScript? By checking user agent?

Feature detection may be impossible: element.focus() strangely does set document.activeElement to element. However, it doesn't cause the on-screen keyboard to pop up, or - if connected - the hardware keyboard to gain focus.

Community
  • 1
  • 1
feklee
  • 7,555
  • 9
  • 54
  • 72
  • 2
    Did you try a test script using feature detection. On page load, dynamically create an element, insert it in to the page, set focus to it, check if focus is set to it and then remove the item. – jfriend00 Apr 03 '13 at 21:01
  • 1
    @jfriend00: I'm looking for something like this indeed. But I don't want to reinvent the wheel, and so haven't done any experiments yet. – feklee Apr 03 '13 at 21:14
  • @jfriend00 I now tried feature detection, and I updated my question. Feature detection seems hard if not impossible. – feklee Apr 03 '13 at 22:41
  • Ahhh, so your question is really about how to get the virtual keyboard to come up for a particular field. You should have mentioned that in your question. – jfriend00 Apr 03 '13 at 23:57
  • @jfriend00 Well, on iOS `el.focus()` does not really do what most, I guess, would consider to be focusing. A `focus()`ed text input field should allow me to type away, but on iOS it doesn't. – feklee Apr 04 '13 at 00:00
  • `el.focus()` apparently does open the virtual keyboard if it is triggered by a script from a user-initiated event (such as the press of a button), just not from your initial page load code or from a timer. It's likely that Apple is trying to protect against something that interfered with iOS usability on web pages that weren't designed for it. – jfriend00 Apr 04 '13 at 00:06
  • @jfriend00 I also assume that this is by design. Still, I need to detect it, to make a web app behave differently in that case. What I consider broken, though, is that `document.activeElement` does get set. – feklee Apr 04 '13 at 00:10

0 Answers0