I'm writing a native plugin to raise an event in JavaScript when the keyboard is showing. I do this:
appView.sendJavascript("cordova.fireWindowEvent('show_keyboard')")
In my JavaScript I then do something like:
window.addEventListener('show_keyboard', handler);
However, this has been flagged as a big no no in PhoneGap by a PhoneGap expert on the team. What is wrong with this approach?
Looking for an answer drawing from credible and/or official sources.
Well I too am no PhoneGap expert, but Apache Cordova, the engine that powers PhoneGap has its source on GitHub.
Well, as an example, let's look at how Cordova sends its volumedownbutton
event. I present lines 613–621 of CordovaWebView.java:
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
this.loadUrl("javascript:cordova.fireDocumentEvent('volumedownbutton');");
return true;
}
// If volumeup key
else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
this.loadUrl("javascript:cordova.fireDocumentEvent('volumeupbutton');");
return true;
}
It would seem that Cordova employs a similar approach to sending events to JavaScript.
I am not sure what the exact issue was that your coworker raised, but it does seem that sendJavascript
is deprecated. So, there's that. But if your appView
is a CordovaWebView
, the you can just call loadUrl
in the same way the that Cordova itself does it (as shown above).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With