I have loaded a webpage in thw WebView and inject jquery and my js file from my asset fole to that webpage. My code is below in short.
MainActivity.java
mWebView.loadUrl("www.test.com/test.html");
mWebView.loadUrl("javascript: " + readFromfile("jquery-1.8.3.js", MainActivity.this));
mWebView.loadUrl("javascript: " + readFromfile("my_android.js", MainActivity.this));
.....................
mWebView.loadUrl("javascript: myMethod();");
my_android.js
function myMethod(){
...............
var mySpan = $("<span id=\"myspan\"></span>");
................
}
This is running fine in pre-Kitkat. But it is giving error in Kitkat. When it is loading the JS, it is giving error:
I/chromium﹕ [INFO:CONSOLE(1)] "Uncaught ReferenceError: varrootjQuery is not defined"
When I am calling the js function, it is giving:
$ is not defined
What is the problem in my code? Here I like to mention all webview related code are running in UI thread only.
I think you just need to wait jquery is loaded before use it.
See this similar problem:
https://stackoverflow.com/a/12763180/1663074