Question

I have a class that draws charts using d3js javascript library. I'm trying to have a global variable to keep track of all the charts drawn, local to my webview, but im loading two webviews in the layout. For some reason, the second chart is always undefined, and I thing it may be a problem with my knowledge in javascript variable scope. Can you please guide me in this task? what should I be looking for? where should I start debugging? Is this a typical problem?

If you need snippets of code, ask me, and I'll update the question. Sorry if this question is too stupid or obvious, but I'm just starting developing in android.

Thanks

Was it helpful?

Solution

Each WebView will have it's own JavaScript context and not share variables with any other WebView. If you want to transfer the value of these variables between WebViews then you'll need to write some code, probably something like this:

  1. Add a JavaScript interface to WebView1 which you can call from JavaScript to get the value of the variable in Java. See http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)
  2. Pass the value into WebView2 using loadUrl with a javascript: URL.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top