Android-活动中的所有Web视图是否共享变量?Webview的范围如何? [英] Android - Do all webviews in an activity share variables? How is the scope in a webview?

查看:73
本文介绍了Android-活动中的所有Web视图是否共享变量?Webview的范围如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用d3js javascript库绘制图表的类.我正在尝试使用一个全局变量来跟踪所有绘制的图表,这些图表在我的Webview中是本地的,但是我在布局中加载了两个Webview.由于某种原因,第二个图表始终是 undefined ,我认为这可能是由于我对javascript变量范围的了解而引起的问题.您能指导我完成这项任务吗?我应该找什么?我应该在哪里开始调试?这是一个典型的问题吗?

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?

如果您需要代码片段,请问我,我将更新问题.抱歉,这个问题太愚蠢或太明显了,但是我只是开始在android中进行开发.

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.

谢谢

推荐答案

每个WebView将拥有自己的JavaScript上下文,并且不会与任何其他WebView共享变量.如果要在WebView之间传递这些变量的值,则需要编写一些代码,可能是这样的:

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. 向WebView1添加一个JavaScript接口,您可以从JavaScript调用该接口以获取Java中变量的值.请参见 http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object ,java.lang.String)
  2. 使用带有 javascript: URL的loadUrl将值传递到WebView2中.
  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.

希望这会有所帮助.

这篇关于Android-活动中的所有Web视图是否共享变量?Webview的范围如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆