I am experiencing an issue when running a script on Safari, specifically WebKit. Here is a sample HTML code:
<!DOCTYPE html>
<html lang="en">
<head>
<script>
function delayedFocus(e) {
e.preventDefault();
setTimeout(function() {
e.target.focus();
}, 1000); // Adjust the delay time as needed (in milliseconds)
}
</script>
</head>
<body>
<ul>
<li>
<input type="text" id="testtext" onmousedown="delayedFocus(event)">
</li>
</ul>
</body>
</html>
The logic behind this script is to introduce a 1-second delay when the user clicks on the textbox. After 1 second, the keyboard should appear. Testing on Android mobile devices shows the expected behavior. However, on iPhones, the textbox receives focus, but the keyboard does not appear. This issue has been observed on various iOS versions, with the script working only on iOS 15.
If you have any insights or solutions to address this compatibility issue, it would be greatly appreciated. Thank you.
2
Answers
@momo Instead of relying on a delay and mousedown event in javascript, you can try using the focus method directly along with a combination of touchstart and click events.After changing this touchstart am able to resolve the keyboard issue. Hope it helps you.
In this code i written that the hidden button is focused when the user clicks on the input field, and the keyboard will appear as expected. After a 1-second delay, the focus is transferred to the actual input field.
The problem you’re having is related to a change in how Safari handles focus events on iOS. When an input field is programmatically focused, Safari now does not show the keyboard immediately. By letting the browser choose when to display the keyboard instead of having it pop up instantly each time an input field is targeted, this is done to improve user experience.
Unfortunately, there is no easy solution for this issue, as it is a change in Safari’s behaviour instead of a bug.