I am trying to load webviews in a view pager.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanc
I was getting Uncaught Reference Error: JavascriptInterfaceName is not defined
on every second startup of my hybrid application on 4.3 and below, and so I did this thanks to @Abi in my WebChromeClient
class:
@Override
@SuppressLint({"AddJavascriptInterface", "InflateParams"})
public boolean onConsoleMessage(@NonNull ConsoleMessage consoleMessage) {
if("Uncaught ReferenceError: JavascriptInterfaceName is not defined".equals(consoleMessage.message())) {
webView.addJavascriptInterface(new WebAppInterface(), "JavascriptInterfaceName ");
webView.reload();
}
return super.onConsoleMessage(consoleMessage);
}
And it works! Thank you very much!
I had a fix on this
just set webChromeClient and catch the error and reload the page...
myWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
isLoading = true;
}
@Override
public void onPageFinished(WebView view, String url) {
myWebView.loadUrl("javascript:testFunction()");
}
});
myWebView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
MessageLevel level = consoleMessage.messageLevel();
if(level.ordinal() == 3) { // Error ordinal
if(loading) {
myWebView.stopLoading();
myWebView.loadUrl(AppConstants.ARTICLE_PAGE_URL);
}
}
}
return false;
}
try something like this and make changes as per your requirement
webview code
web.getSettings().setJavaScriptEnabled(true);
JavaScriptInterface JSInterface = new JavaScriptInterface(this);
web.addJavascriptInterface(JSInterface, "JSInterface");
setContentView(web);
web.loadUrl("file:///android_asset/demo.html");
javascript interface
public class JavaScriptInterface {
Context mContext;
/** Instantiate the interface and set the context */
JavaScriptInterface(Context c) {
mContext = c;
}
public void showToast()
{
Toast.makeText(mContext, "Button Clicked", Toast.LENGTH_SHORT).show();
}
}
webpage code
<!DOCTYPE html>
<html>
<head>
<script>
var i=0;
function myFunction()
{
JSInterface.showToast();
}
</script>
</head>
<body>
<button onclick="myFunction()">Try it</button>
<p>By clicking the button above, a function will be called. The function will alert a message.</p>
</body>
</html>
EDIT
function myFunction()
{
do here somthing
}