0

I'm calling a JavaScript function from my application's WebView. This function returns (if i call it from firebug) for example:

Object {top: 350.171875, left: 129.265625, w: 311, h: 115}

What i want is to get the values of top, left, w and h from my Android application. But when I try to get the values through WebChromeClient onJsAlert() method the value of message parameter is [object Object]. Is there a way to extract the required values of top, left, w, h from [object Object]? i can't find a way to parse [object Object].

4

2 回答 2

1

通常,您可以使用 JavascriptInterface(请参阅http://developer.android.com/guide/webapps/webview.html)。

这可以是您的回调接口:

public class MyInterface {
    @JavascriptInterface
    public void myCallback(int top, int left, int w, int h) {
        // This is where you receive the values
    }
}

这是您绑定它的方式:

myWebView.addJavascriptInterface(new MyInterface(), "Android");
myWebView.loadUrl("javascript:someJsFunction()");

这就是 javascript 发送值的方式:

function someJsFunction() {
    Android.myCallback(dimensions.top, dimensions.left, dimensions.w, dimensions.h);
}

您不能将实际对象从 javascript 传递给 android。您只能传递它们的原语、字符串和数组。

于 2013-09-05T10:17:43.410 回答
1

您应该能够简单地做dimensions.topordimensions.left等​​(假设对象存储在变量中dimensions

var dimensions = {top: 350.171875, left: 129.265625, w: 311, h: 115}; //Would actually be a call to your function to calculate these dimensions
alert(dimensions.top);
于 2013-09-05T08:17:03.647 回答