0

I am working on a Phonegap application . Here I want to access java variables in javascript code .I can access functions from javacode to javascript code here.So I think it should be possible to access java variables from javacode to javascript code . How can I do that . I have visited many post on Overflow but did't help .

I am putting sample code here .

JavaCode

class A
{
    String a, b ;
    A()
    {
       a = "phonegap";
       b=  "application" ;
    }

}

Javascript Code :

<HTML>
  <script type= "text/javascript">

  //Here I want to access java variables a and b .

  </script>

Community
  • 1
  • 1

1 Answers1

0

java

myWebView.loadUrl("javascript:callJS('things from java')")

js

function callJS(str){
  console.log(str)
}
suish
  • 3,253
  • 1
  • 15
  • 34
  • 1
    Maybe a few explaining sentences would help... :-) – Marcus Rickert Sep 20 '14 at 09:39
  • @suish It's giving error, callJS not defined . I am putting callJS inside index.js . –  Sep 20 '14 at 09:47
  • double check the function you call from webview is on global.I assume your callJS function is wrapped by another function.if you can USB devices debug of chrome, check you can call callJS from chrome console or not. – suish Sep 20 '14 at 09:55
  • yes , It is on global, It is not wrapped by another function. –  Sep 20 '14 at 10:01
  • I am implementing it inside a phonegap app .Here CordovaWebView is used I am calling loadUrl() from app's starting point (Java file) and I am putting callJS inside index.js which is used by index.html .Here I am assuming that as loadUrl() will be called, callJs will also be called from index.js file . –  Sep 20 '14 at 10:09