Skip to content Skip to sidebar Skip to footer

Android WebView - Detect Whether A JS Function Is Defined

I'm writing an HTML/JS application that runs under a WebView on Android. Under certain circumstances the page containing the application may not be loaded, or another page might in

Solution 1:

@JavascriptInterface
public void onLoaded(String result) {
    if (result.equals("function")) {
        // defined
    } else {
       // not defined
    }
}

private void checkJSFunction() {
    loadUrl("javascript:yourJSInterfaceName.onLoaded(typeof yourFunctionName)");
}

update: this code will check the JS function define by "typeof" and return its result to java through javascript interface "onLoaded".


Solution 2:

I think you can do it, Please look at PhoneGap project, you will find your answer.

PhoneGap is an awesome free framework for mobile platform.


Post a Comment for "Android WebView - Detect Whether A JS Function Is Defined"