in iOS8 using .focus() will show virtual keyboard and scroll page after touch in iOS8 using .focus() will show virtual keyboard and scroll page after touch ios ios

in iOS8 using .focus() will show virtual keyboard and scroll page after touch


It looks like you're definitely hitting an iOS 8 bug. In iOS7, Safari would (apparently) ignore or keep unfocused elements that had focus set prior to page load. This includes both <input autofocus> and input.focus() that occur up to some point, possibly page load (I tested just with an inline script).

In iOS 8, Safari is now apparently remembering that the element was focussed but not actually focussing it until a touch down event. It is then blindly sending a click event to whichever element received the touch up.

Both browsers behave the same for input.focus() occurring after page load. They both zoom to the element and bring up the keyboard.

Tests:

The good news is that you only need to be worried about new behavior on elements you want to prefocus. The other good news is that while you will have to use a user-agent workaround, you can use it for all iOS versions since they were already behaving like you weren't autofocusing:

if (!/iPad|iPhone|iPod/g.test(navigator.userAgent)) {    element.focus();}

This appears to be the approach http://www.google.com uses based on some basic user-agent testing:

  • Mac Book Pro: autofocus before page load.
  • iPhone: no autofocus
  • iPad: no autofocus
  • Kit Kat (Android): focus after page load, possibly doing extra detection for presence of software keyboard.

If you haven't, you should go ahead and file a radar with Apple at https://bugreport.apple.com.


If you are developing a Cordova project, you can fix it adding this line

<preference name="KeyboardDisplayRequiresUserAction" value="false" />

to your config.xml file. Tested in IOS 8.3 and IOS 8.4


It seems that in iOS 8 there has been an API change on the default handling for the javascript focus() command. If your application is a hybrid app in which you have direct control over Apple's web view facade the below is directly from apples docs.

A Boolean value indicating whether web content can programmatically display the keyboard.

[myWebView setKeyboardDisplayRequiresUserAction:YES];

When this property is set to YES, the user must explicitly tap the elements in the web view to display the keyboard (or other relevant input view) for that element. When set to NO, a focus event on an element causes the input view to be displayed and associated with that element automatically.

The default value for this property is YES.

From the last paragraph it seems this method call is not strictly for the keyboard. It indicates that it is for input views across the board i.e. drop down and date picker etc.

It seems though there is a bug as this method call is not currently working for me. The current behavior I am receiving corresponds as if it defaults to NO.