机器人的WebView跳过的JavaScript即使setJavascriptEnabled(真)和WebChromeClient [英] Android webview SKIPS javascript even with setJavascriptEnabled(true) and WebChromeClient

查看:924
本文介绍了机器人的WebView跳过的JavaScript即使setJavascriptEnabled(真)和WebChromeClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(使用三星Galaxy Tab和Android 3.0,这是为了工作,每一个3.0+平板电脑,就像它在他们的浏览器,只是没有web视图)

(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)

我有一个CSS和jQuery加载罚款,浏览器,浏览器的Andr​​oid和iOS设备的网页,但是Android的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.

我已经缩小的问题它只是跳过的JavaScript负载(通过复制一些的WebView的JavaScript应用程序演示该问题)。这是为什么?

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?

下面是完整的HTML测试code http://pastebin.com/GmE2vEYx

HERE IS THE COMPLETE TEST HTML CODE http://pastebin.com/GmE2vEYx

和这里是我把它叫做:

  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");

有一件事我注意到的是,如果你给它时间(通过暂停在调试器断点的应用程序),那么的JavaScript会更一致加载。这使我试图使应用程序加载速度慢一些牵强的实验,但这是真的不想要的用户体验。我真的希望的Andr​​oid的WebView真的不是移动开发的IE6。

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.

补充资料:

如果您删除<脚本> 标签包含所有的jQuery,那么页面加载所有的CSS格式的快。一旦jQuery是添加,没有加载在所有。

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.

我还发现了一些遇到相同问题的其他问题,很多置之不理或没有确凿的答案是什么OP是寻找:(

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 :(

请参阅: <一href="http://stackoverflow.com/questions/5110916/android-cant-get-javascript-to-work-on-webview-even-with-setjavascriptenabledt">Android:不能得到的javascript甚至与setJavaScriptEnabled工作的WebView(真)

<一个href="http://stackoverflow.com/questions/6207093/page-reload-webview-loadurl-results-in-javascript-not-being-fully-processed">Page重装(Webview.loadUrl)导致的Javascript不是(完全)处理

<一个href="http://groups.google.com/group/android-developers/browse_thread/thread/9962064ef217a5a7#">http://groups.google.com/group/android-developers/browse_thread/thread/9962064ef217a5a7#

<一个href="http://stackoverflow.com/questions/3306501/javascript-sometimes-doesnt-work-in-androids-webview">JavaScript有时不能工作Android的WebView功能

<一个href="http://stackoverflow.com/questions/2968936/android-webview-not-loading-a-javascript-file-but-android-browser-loads-it-fine">Android的WebView不加载JavaScript文件,但Android浏览器加载它精致

<一个href="http://stackoverflow.com/questions/3450812/androids-webview-cannot-handle-javascript">Android's web视图不能处理JavaScript?

建议?让我们得到的这条底线! Android的文件说,web视图有一定的局限性,但我不知道该限制的,因为人们将HTML CSS JS应用服务所有的时间!

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!

推荐答案

答案是JavaScript需要在HTML页面&LT内的功能;脚本&GT; 部分然后需要与被注入 webview.loadUrl(JavaScript的:yourJavascriptFunction())webview.setWebviewClient后(新WebviewClient(){公共无效onPageFinishedLoading(){... // JS注射这里});

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 });

这篇关于机器人的WebView跳过的JavaScript即使setJavascriptEnabled(真)和WebChromeClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆