即使使用 setJavascriptEnabled(true) 和 WebChromeClient,Android webview 也跳过 javascript [英] Android webview SKIPS javascript even with setJavascriptEnabled(true) and WebChromeClient
问题描述
(使用三星 Galaxy Tab 和 Android 3.0,这旨在适用于所有 3.0+ 平板电脑,就像在浏览器中一样,只是不适用于 webview)
我有一个带有 CSS 和 jQuery 的页面,可以在浏览器、Android 浏览器和 iOS 设备中正常加载,但 android webview 拒绝始终如一地加载它.
我已经将问题缩小到只是在加载时跳过 javascript(通过使用一些 webview javascript 演示应用程序复制问题).这是为什么?
这是完整的测试 HTML 代码http://pastebin.com/GmE2vEYx
我是这么称呼它的:
myWebview.loadUrl("file:///android_asset/myPage.html");myWebview.getSettings().setJavaScriptEnabled(true);myWebview.setWebChromeClient(new WebChromeClient());
这三个调用的顺序不会影响结果.
myWebview.getSettings().setJavaScriptEnabled(true);myWebview.setWebChromeClient(new WebChromeClient());myWebview.loadUrl("file:///android_asset/myPage.html");
我注意到的一件事是,如果您给它时间(通过在调试器中使用断点暂停应用程序),那么 javascript 将更加一致地加载.这让我进行了一些牵强附会的实验,试图使应用程序加载速度变慢,但这确实不是理想的用户体验.我真的希望 Android webview 真的不是移动开发的 IE6.
其他事实:
如果删除包含所有 Jquery 的 <script>
标记,则页面会快速加载所有 CSS 格式.添加 Jquery 后,根本没有加载任何内容.
我还发现了其他几个遇到完全相同问题的问题,许多问题没有得到解答,或者对 OP 所寻求的内容没有确定的答案:(
见:Android:无法使用 javascriptWebView 即使使用 setJavaScriptEnabled(true)
页面重新加载 (Webview.loadUrl)导致 Javascript 未被(完全)处理
http://groups.google.com/group/android-developers/browse_thread/thread/9962064ef217a5a7#
JavaScript 有时在 android 的 webview 中不起作用>
Android WebView 不加载 JavaScript 文件,但 Android 浏览器加载正常
Android 的 Webview 无法处理 javascript?
建议?让我们深入了解这件事!android 文档说 webview 有限制,但我不知道限制在哪里,因为人们一直在制作 HTML CSS JS 应用程序!
答案是javascript需要在HTML页面sections内的函数中,然后需要注入和
webview.loadUrl("javascript:yourJavascriptFunction()") 在 webview.setWebviewClient(new WebviewClient(){ public void onPageFinishedLoading(){ ...//js 注入这里 });
(using Samsung Galaxy Tab and Android 3.0, this is intended to work on every 3.0+ tablet, like it does in their browsers, just not webview)
I have a page with CSS and jQuery that loads fine in browsers, android browsers and iOS devices, but android webview refuses to load it consistently.
I've narrowed down the problem to it simply skipping javascript on load (by replicating the problem with some webview javascript demo apps). Why is this?
HERE IS THE COMPLETE TEST HTML CODE
http://pastebin.com/GmE2vEYx
and here is how I call it:
myWebview.loadUrl("file:///android_asset/myPage.html");
myWebview.getSettings().setJavaScriptEnabled(true);
myWebview.setWebChromeClient(new WebChromeClient());
The ordering of these three calls doesn't effect the result.
myWebview.getSettings().setJavaScriptEnabled(true);
myWebview.setWebChromeClient(new WebChromeClient());
myWebview.loadUrl("file:///android_asset/myPage.html");
One thing I've noticed is that if you give it time (by pausing the app with a breakpoint in the debugger) then the javascript will more consistently load. This led me to some farfetched experiments of trying to make the app load slower, but this is really not the desired user experience. I'm really hoping that Android webview really aren't the IE6 of mobile development.
Additional facts:
If you remove the <script>
tags that contain all of the Jquery, then the page loads quickly with all of the CSS formatting. As soon as the Jquery is added, nothing loads at all.
I have also found several other questions that encounter the exact same problem, many go unanswered or have no conclusive answer to what the OP was seeking :(
see: Android: can't get javascript to work on WebView even with setJavaScriptEnabled(true)
Page reload (Webview.loadUrl) results in Javascript not being (fully) processed
http://groups.google.com/group/android-developers/browse_thread/thread/9962064ef217a5a7#
JavaScript sometimes doesn't work in android's webview
Android WebView not loading a JavaScript file, but Android Browser loads it fine
Android's Webview cannot handle javascript?
suggestions? lets get to the bottom of this! The android documentation says the webview has limitations but I don't know where that limit is, since people make HTML CSS JS apps all the time!
The answer is that the javascript needs to be in functions within the HTML page <script>
sections and then needs to be injected with
webview.loadUrl("javascript:yourJavascriptFunction()") after webview.setWebviewClient(new WebviewClient(){ public void onPageFinishedLoading(){ ...//js injections here });
这篇关于即使使用 setJavascriptEnabled(true) 和 WebChromeClient,Android webview 也跳过 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!