admin管理员组文章数量:1313347
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!
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!
Share Improve this question asked Jan 27, 2016 at 15:44 btxbtx 2,4973 gold badges26 silver badges39 bronze badges1 Answer
Reset to default 11First 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
本文标签: javaHow to check if WebView Javascript Interface is availableStack Overflow
版权声明:本文标题:java - How to check if WebView Javascript Interface is available? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741934014a2405742.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论