开发者

How to prevent a JS blur event in iOS webkit when the virtual keyboard is dismissed?

开发者 https://www.devze.com 2023-02-24 04:30 出处:网络
This question is about web apps, not native code. I\'m adapting a web application as best I can using media queries. Later, I may have time to re-build it from the ground up. For historical reasons,

This question is about web apps, not native code.

I'm adapting a web application as best I can using media queries. Later, I may have time to re-build it from the ground up. For historical reasons, this application uses Prototype and Scriptaculous. It uses the autocompleter UI class.

In Android webkit, at least on Android 2.2/HTC Evo, when I dismiss the software keyboard, the autocompleter remains and I'm able to choose from the options presented. I believe iOS is shooting off the "blur" event when I click DONE on the virtual keyboard, which causes my results to instantly disappear from the screen.

I'm looking for solutions to this problem in descending order of preference:

1) A webkit property I can set on the input that prevents the blur from being fired

2) Any existing Javascript workarounds that might be available, for example, any properties of the event objec开发者_运维问答t I might be able to utilize to re-focus the element after a virtual keyboard dismissal [what I don't want to do is onblur re-focus because that will really screw with the non-touch clients of this site]

3) Other creative ideas you may have, not including JS browser detection and ugly hacking on my code [I don't need your help with that].


After a bit of searching, I don't think that WebKit offers such a property (I might be wrong). Here's a simple Vanilla JS workaround.

var userAgent = navigator.userAgent.toLowerCase();

if (userAgent.search('iphone') > -1 || userAgent.search('ipod') > -1)
{
  document.getElementById('your_textbox').onblur = function() { this.focus(); };
}

This solution does perform a focus() even after the textbox launches its onblur trigger, but it only does this for MobileSafari on iPhone and iPod Touch (iPad might be similar).

0

精彩评论

暂无评论...
验证码 换一张
取 消