Mobile Safari Autofocus text field
I think this is a feature of mobile Safari rather than a bug. In our work on FastClick, my colleagues and I found that iOS will only allow focus to be triggered on other elements, from within a function, if the first function in the call stack was triggered by a non-programmatic event. In your case, the call to setTimeout
starts a new call stack, and the security mechanism kicks in to prevent you from setting focus on the input.
Remember that on iOS setting focus on an input element brings up the keyboard - so all those web pages out there that set focus on an input element on page load, like Google does, would be extremely annoying to use on iOS. I guess Apple decided they had to do something to prevent this. So I disagree with @DA: this is a feature not a bug.
There's no known workaround for this, so you'll have to ditch the idea of using a delay.
Update August 2012:
As of iOS 5, handlers triggered by synthesised click events are allowed to trigger focus on input elements. Try the updated FastClick input focus example.
How can you autofocus on a form field in iPhone Safari?
According to this page, autofocus is not supported in iphone/ipad for usability reasons.
Can't Set Focus in Safari
EDITED TO INCLUDE .focus()
Try
javascript:document.getElementById('identNum').focus().setSelectionRange(0, 999);
Mobile devices, in particular iOS can be quite funny about select();
https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setSelectionRange
EDIT: A note on input focus without user interaction
As found by @zappullae
It appears that in recent versions of iOS, Apple require user interaction in order to activate the keyboard, so this will not be possible on page load.
https://medium.com/@brunn/autofocus-in-ios-safari-458215514a5f
Set textbox focus in mobile safari
Just found this previous post stating this is a bug in webkit. Sorry for not doing a more thorough search.
http://discussion.forum.nokia.com/forum/showthread.php?t=127724
Related Topics
Can a PDF File's Print Dialog Be Opened with JavaScript
How to Check If Iframe Is Loaded or It Has a Content
How to Find First Element of Array Matching a Boolean Condition in JavaScript
How to Profile JavaScript Execution
How to Parse a Time into a Date Object from User Input in JavaScript
Are Braces Necessary in One-Line Statements in JavaScript
Scrolling an Iframe with JavaScript
How Does Asynchronous JavaScript Execution Happen? and When Not to Use Return Statement
Regular Expression for Password Validation
Create Svg Tag with JavaScript
Returning Chrome Storage API Value Without Function
Typeerror: Console.Log(...) Is Not a Function
Rails - Could Not Find a JavaScript Runtime
Are Es6 Classes Just Syntactic Sugar for the Prototypal Pattern in JavaScript
How to Share Code Between Node.Js and the Browser