Android webview javascript only loads sometimes
I have javascript in a webview that only loads sometimes. Other times it doesn't, this effects my page layout and functionality.
the obvious answer myWebView.getSettings().setJavaScriptEnabled(true);
is already being used before my loadurl
function
right now, both the html file and the jquery library file are being loaded from the assets
folder. In the HTML code I am using <script type="text/javascript" src="jquery.min.js"></script>
to load the jquery file locally
On the Java side within my onCreate() method:
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
myWebView.loadUrl("file:///android_asset/newtry.html");
The HTML file
<htm开发者_运维技巧l>
<head>
<script type="text/javascript" src="jquery.min.js"></script>
<script> //the custom jquery functions, no more includes </script>
<style type="text/css">
//the css
</style>
</head>
<body>
these files - the HTML and the jquery.min.js are in the same folder assets
what else can I do?
Have you tried to clear the cache and clear the history of the webview? I got my javascript to start loading again if I called those two right before I call loadUrl.
The answer is that you have to force the app to wait (try it, place a breakpoint at your webview, and then let it continue, you'll see the webview will load with javascript this time, reliably. Whereas if you just let the app load without the breakpoint, it frequently "skips" the javascript). Some sort of forced threaded wait method is needed.
A second method is to force the javascript to load after the page is loaded, as shown by the author's answer here android: webview not loading javascript with custom WebViewClient
精彩评论