Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery doesn't work on WebView

I tried to include JQuery files in assets/scripts and on Internet, but the alert dialog doesn't show. I got the log output and make it output.html, it works in Windows (so strange!). What's the problem with WebView?

public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    webView = (WebView) findViewById(R.id.webView);
    final String s = "<html><head>" +
    "<link href=\"css/my.css\" type=\"text/css\" rel=\"stylesheet\" />" +
    "<script src=\"scripts/jquery-1.6.2.min.js\" rel=\"stylesheet\" type=\"text/javascript\"></script>" +
    "<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js\" type=\"text/javascript\"></script>" +
    "<script>" +
    "$(document).ready(function(){ alert('hello'); });" +
    "</script>" +
    "</head><body><div>All I hear is raindrops." +
    "Falling on the rooftop. Oh baby tell me why you have to go. " +
    "Cause this pain I feel you won't go away. And today, " +
    "I'm officially missing you.</div></body></html>";
    webView.getSettings().setJavaScriptEnabled(true);
    Log.d("Something", s);
    webView.loadDataWithBaseURL("file:///android_asset/", s, "text/html", "utf-8", null);
}

This is the log output after adding extension ".html". It works on Firefox but does not on WebView. :(

<html>
<head>
<link href="css/my.css" type="text/css" rel="stylesheet" />
<script src="scripts/jquery-1.6.2.min.js" rel="stylesheet" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js" type="text/javascript"></script>
<script>$(document).ready(function(){ alert('hello'); });</script>
</head>
<body>
    <div>
    All I hear is raindrops.Falling on the rooftop. Oh baby tell me why you have to go. Cause this pain I feel you won't go away. And today, I'm officially missing you.
    </div>
</body>
</html>
like image 722
emeraldhieu Avatar asked Jul 19 '11 17:07

emeraldhieu


4 Answers

webView.loadDataWithBaseURL("file:///android_asset/", s, "text/html", "utf-8", null);

be change to

webView.loadDataWithBaseURL(getAssets(), s, "text/html", "utf-8", null);

to get asset file, you will need to access app's asset path. An app is an user on Android, so cannot access path begin with "file://" directory.

like image 174
wktk Avatar answered Nov 05 '22 18:11

wktk


You will need to have the jquery.js file in your assets/scripts folder for this to work.

scripts/jquery-1.6.2.min.js

That should work.

like image 34
Kumar Bibek Avatar answered Nov 05 '22 16:11

Kumar Bibek


As already mentioned on Android WebView doesn't load jQuery:

Where is your scripts/jquery-1.6.2.min.js script located? If it is located in your assets directory, then you should initialize the webView giving it the assets directory as baseUrl:

webView.loadDataWithBaseURL("file:///android_asset/", data, "text/html", "UTF-8", null);

or

webView.loadUrl("file:///android_asset/file.html");

You could try to create a simple .js file with a simple function like

function dummy(document) { document.write("Hooray it works"); }

and try to access the dummy function in your html to test if the .js file is included.

like image 2
Wojciech Górski Avatar answered Nov 05 '22 16:11

Wojciech Górski


setPluginsEnabled in WebView settings to true.

like image 2
raja Avatar answered Nov 05 '22 17:11

raja