Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android webview SKIPS javascript even with setJavascriptEnabled(true) and WebChromeClient

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

like image 736
CQM Avatar asked Aug 04 '11 23:08

CQM


2 Answers

I also have Samsung Galaxy Tab and Android 3.0 with jQuery javascript webpage and it works. Be sure your page is working in Crome browser and that you don't have javascript errors. There are known issues with some CSS3 styling (e.g. images disappearing) but jQuery should work. If you try to communicate with javascript be sure you do it after the page is loaded. Be seure also all files are in the right directory :). Also sometimes the content is cached so try to delete it (or delete the app).

Please provide the source code of your webpage so we can check.

Sample webview:

webView = (WebView) findViewById(R.id.webview);

WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setPluginsEnabled(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setSupportZoom(false);

webView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
webView.setWebViewClient(new WebViewClient()
{
    @Override
    public void onPageFinished(WebView view, String url) 
    {
        // page loaded...
        super.onPageFinished(view, url);
    }
});
webView.setWebChromeClient(new WebChromeClient()
{
    @Override
    public boolean onJsAlert(WebView view, String url, String message,JsResult result) 
    {
        Log.e("alert triggered", message);
        return false;         
    }
});
webView.loadUrl("http://mypage/mypage.html");

I hope it helps.

UPDATE1: I tested your code on Samsung Galaxy tab. I changed

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>

line. The page is loaded, I can see the content with picture. So... the code is working as expected. I noticed that sometimes the content is not displayed as it should be. It seams that the zooming is the problem:

<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, user-scalable=no" />

and the calculation inside $(document).ready() is false!

Btw... what you are looking for is the ViewPager with WebViews.

UPDATE2: Note also when loading webpage as file:/// you have to have JS files in the same directory (no external URLs).

like image 65
xpepermint Avatar answered Oct 23 '22 20:10

xpepermint


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

like image 38
CQM Avatar answered Oct 23 '22 21:10

CQM