Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
131
rated 0 times [  134] [ 3]  / answers: 1 / hits: 31805  / 13 Years ago, sun, september 25, 2011, 12:00:00

How do we call javascript from Android? I have this javascript library which I would like to use, I want to call the javascript function and pass the result value to the android java code. Haven't found the answer from now. i managed to call android code from javascript, but I want the other way around.


More From » java

 Answers
15

There is a hack:




  1. Bind some Java object so that it can be called from Javascript with WebView:



    addJavascriptInterface(javaObjectCallback, JavaCallback)

  2. Force execute javascript within an existing page by



    WebView.loadUrl(javascript: var result = window.YourJSLibrary.callSomeFunction();
    window.JavaCallback.returnResult(result));



(in this case your java class JavaObjectCallback should have a method returnResult(..))



Note: this is a security risk - any JS code in this web page could access/call your binded Java object. Best to pass some one-time cookies to loadUrl() and pass them back your Java object to check that it's your code making the call.


[#89940] Thursday, September 22, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ira

Total Points: 298
Total Questions: 112
Total Answers: 103

Location: Guadeloupe
Member since Sat, Aug 22, 2020
4 Years ago
;