I have coded a game in HTML and JavaScript. I had loaded the HTML file locally into the WebView but it had not loaded the JavaScript. I was wondering how I could get the HTML to use the JavaScript. Any help is greatly appreciated.
5 Answers
You can check here: http://developer.android.com/resources/tutorials/views/hello-webview.html
in order to get a general perspective of js with webview and more specifically:
mWebView.getSettings().setJavaScriptEnabled(true);
Hope this helps!
2 Comments
Ben T
It doesn't work. the HTML loads 2 javascripts locally. Is there another way?
Dimitris Makris
Please add some code in order to better understand your problem.
you can try this :
String html = "<html><body>Hello, World!</body></html>";
String mime = "text/html";
String encoding = "utf-8";
WebView myWebView = (WebView)this.findViewById(R.id.myWebView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadDataWithBaseURL(null, html, mime, encoding, null);
Comments
binding.webview.settings.javaScriptEnabled=true
binding.webview.loadUrl("file:///android_asset/GeneralCF.html")
binding.your_view.setOnClickListener {
// load js on click button
binding.webview.loadUrl("javascript:myFunction(\"$str_name\")")
}
index.js
function myFunction(str_name)
{
document.getElementById("demo").innerHTML = str_name;
}
GeneralCF.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script src="index.js"></script>
</head>
<body>
<h1>This is my first webpage</h1>
<p id="demo">A Paragraph.</p>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>