BTAndroidWebViewSelection
BTAndroidWebViewSelection copied to clipboard
Javascript call from WebView to Android and Vice Versa
Good Day,
I have successfully executed this sample project using Eclipse, but i have this problem... When I add a simple button in the html file provided and attach an event to that button(I would like to call a javascript function), the javascript is not triggering.
Also, I tried to call a javascript function inside the BTWebView
class at the OnActionItemClickListener
whenever an item is clicked, calling a javascript using this:
this.loadUrl("javascript: testFunction();"); but it's not triggering.
Hope to hear from you Soon.
Wrap your javascript in a try catch block with the error method and see if anything prints.
try{ // Your Code } catch(err){ window.TextSelection.jsError(err); }
That should log an error in eclipse if there's a javascript error somewhere. Good place to start. Aside from that, send me an example of your html content/custom js and I'll try and take a look.
Thanks for replying, I have done what you said but still does nothing.
Here's the OnActionItemClickListener
with little changes.
mContextMenu.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() { public void onItemClick(QuickAction source, int pos, int actionId) { // TODO Auto-generated method stub if (actionId == 1) { // Do Button 1 stuff Log.i(TAG, "Hit Button 1: "); loadUrl("javascript: prom()"); // Here it is, I'm trying to call a javascript function on the html page provided } else if (actionId == 2) { // Do Button 2 stuff Log.i(TAG, "Hit Button 2"); } else if (actionId == 3) { // Do Button 3 stuff Log.i(TAG, "Hit Button 3"); } contextMenuVisible = false; } });
No error is being displayed in my Eclipse log cat from a WebView Tag category. Still I did tried pressing the button but the javascript function in my page wont trigger.
Thanks in advance.
I just had this problem too. If you're building for Android API 17 or greater you need to add the @JavascriptInterface annotation to the methods in TextSelectionJavascriptInterface, otherwise they're not available to the webview and the javascript fails with "TypeError: Object [Object] has no method 'setContentWidth'".
Thanks for that. I just updated the class on the master branch with the @JavascriptInterface annotation.
You're welcome, and thanks for the interesting demo.