admin管理员组文章数量:1292172
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.
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.
Share Improve this question edited Jan 6, 2012 at 19:46 cdeszaq 31.3k27 gold badges122 silver badges175 bronze badges asked Oct 25, 2011 at 20:27 Ben TBen T 551 gold badge3 silver badges6 bronze badges 1- Where do you put the HTML and Javascript currently? Within assets folder? – momo Commented Oct 25, 2011 at 21:32
5 Answers
Reset to default 4Try this code.. This code works for.. Hope it will be helpfull to u also.
WebView webView;
webView = (WebView) findViewById(R.id.wbView);
webView.getSettings().setPluginsEnabled (true);
webView.getSettings().setJavaScriptEnabled (true);
You can check here: http://developer.android./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!
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);
Just load HTML file URI like this:
webView.loadUrl("file://" + getContext().getExternalCacheDir() + "/editor.html");
Hope it can help you!
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>
本文标签: How to load local HTML with JavaScript in Android WebViewStack Overflow
版权声明:本文标题:How to load local HTML with JavaScript in Android WebView? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741543658a2384466.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论