is there any proper way to check in JavaScript inside the WebView, if the JavaScript Interface is available/defined?
My Idea was to define a checking method in the interface, that (if available) could be called and simply return a true. Isn't there a proper 'official' way to do that? Because if the interface is not available/undefined, it will throw an error.
I'm using Android, that's why Java code:
webView.addJavascriptInterface(new WebAppinterface(this),"InterfaceName");
@JavascriptInterface
public void isInterfaceAvailabe (){
return true;
}
And in Javascript:
function hasJavaScriptInterface () {
if (InterfaceName.isInterfaceAvailable()){
return true;
}
else {
return false;
}
}
Thanks for answers!
First you need to check whether InterfaceName
is actually exported, that is -- does it present as a property on the window
object. You can do it several ways:
if ("InterfaceName" in window) ...
or
if (window.InterfaceName) ...
or
if (typeof window.InterfaceName === "function") ...
Because if you just try invoking a non-defined property, you will get an error: Uncaught TypeError: window.InterfaceName is not a function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With