Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding to a touchstart event causing autofocus to focus on any click in Mobile Safari

Autofocus is, in theory, completely unsupported in mobile Safari (http://caniuse.com/#feat=autofocus). We're seeing, however, this very strange behaviour where when a touchstart event is bound to document (whether the function has any behaviour or not) when a click happens anywhere on the page the first input with autofocus becomes focused and the onscreen keyboard displays.

This can be reproduced using iOS 8 in the iOS simulator. Here is the minimal html and javascript to reproduce the problem:

<html>
<head>
<script>
document.addEventListener("touchstart", function() {});
</script>
</head>

<body>
<input type="text">
<input type="text" autofocus="autofocus">
</body>
</html>

And here's a jsfiddle: http://jsfiddle.net/qd858nob/

Simply click anywhere outside of the inputs, and the second input will become autofocused. Ideally I'd love to find a global solution that resolves this without having to remove autofocus from all of our input elements.

like image 473
Ben Zittlau Avatar asked Nov 01 '22 14:11

Ben Zittlau


1 Answers

See in iOS8 using .focus() will show virtual keyboard and scroll page after touch for a broader discussion on this issue. It looks like it's an iOS 8 "bug". I've filed a report with Apple -- best you can do is the same and hope it bubbles high enough up on their list to fix soon.

like image 100
Dan Avatar answered Nov 09 '22 05:11

Dan