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?
我试图将JQuery文件包含在资源/脚本和Internet上,但是警告对话框没有显示。我得到了日志输出并使其成为output.html,它在Windows中工作(太奇怪了!)。 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”后的日志输出。它适用于Firefox,但不适用于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>
6 个解决方案
#1
7
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.
要获取资产文件,您需要访问应用程序的资产路径。应用程序是Android上的用户,因此无法访问以“file://”目录开头的路径。
#2
2
You will need to have the jquery.js file in your assets/scripts folder for this to work.
您需要在assets / scripts文件夹中包含jquery.js文件才能使其正常工作。
scripts/jquery-1.6.2.min.js
脚本/ jQuery的1.6.2.min.js
That should work.
这应该工作。
#3
2
As already mentioned on Android WebView doesn't load jQuery:
正如Android WebView上已经提到的那样,不加载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:
您的scripts / jquery-1.6.2.min.js脚本位于何处?如果它位于您的资产目录中,那么您应该初始化webView,将其资产目录作为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
您可以尝试使用简单的函数创建一个简单的.js文件
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.
并尝试访问html中的虚拟函数来测试是否包含.js文件。
#4
2
setPluginsEnabled
in WebView
settings to true
.
WebView设置中的setPluginsEnabled为true。
#5
2
I Guess, some javascript functions like prompt and alert (System popups), should be implemented by your code. A simple guide..,
我猜,一些javascript函数,如提示和警报(系统弹出窗口),应该由您的代码实现。一个简单的指南..,
1. Create a class Jscalls.java
1.创建一个类Jscalls.java
public class Jscalls {
Context mContext;
Jscalls(Context c) {
mContext = c;
}
/** Show a toast from the web page */
public void alert(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
2. In your program add, webView.addJavascriptInterface(new Jscalls(this), "Android");
2.在你的程序中添加webView.addJavascriptInterface(新的Jscalls(this),“Android”);
3. In html pages, instead of alert("hello")
, use Android.alert("hello")
3.在html页面中,使用Android.alert(“hello”)而不是alert(“hello”)
Hope it works :)
希望它有效:)
#6
1
You should give full path to load javascript and css for example
你应该给出加载javascript和css的完整路径
<link href="http://yourdomain.com/css/my.css" type="text/css" rel="stylesheet" />
#1
7
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.
要获取资产文件,您需要访问应用程序的资产路径。应用程序是Android上的用户,因此无法访问以“file://”目录开头的路径。
#2
2
You will need to have the jquery.js file in your assets/scripts folder for this to work.
您需要在assets / scripts文件夹中包含jquery.js文件才能使其正常工作。
scripts/jquery-1.6.2.min.js
脚本/ jQuery的1.6.2.min.js
That should work.
这应该工作。
#3
2
As already mentioned on Android WebView doesn't load jQuery:
正如Android WebView上已经提到的那样,不加载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:
您的scripts / jquery-1.6.2.min.js脚本位于何处?如果它位于您的资产目录中,那么您应该初始化webView,将其资产目录作为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
您可以尝试使用简单的函数创建一个简单的.js文件
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.
并尝试访问html中的虚拟函数来测试是否包含.js文件。
#4
2
setPluginsEnabled
in WebView
settings to true
.
WebView设置中的setPluginsEnabled为true。
#5
2
I Guess, some javascript functions like prompt and alert (System popups), should be implemented by your code. A simple guide..,
我猜,一些javascript函数,如提示和警报(系统弹出窗口),应该由您的代码实现。一个简单的指南..,
1. Create a class Jscalls.java
1.创建一个类Jscalls.java
public class Jscalls {
Context mContext;
Jscalls(Context c) {
mContext = c;
}
/** Show a toast from the web page */
public void alert(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
2. In your program add, webView.addJavascriptInterface(new Jscalls(this), "Android");
2.在你的程序中添加webView.addJavascriptInterface(新的Jscalls(this),“Android”);
3. In html pages, instead of alert("hello")
, use Android.alert("hello")
3.在html页面中,使用Android.alert(“hello”)而不是alert(“hello”)
Hope it works :)
希望它有效:)
#6
1
You should give full path to load javascript and css for example
你应该给出加载javascript和css的完整路径
<link href="http://yourdomain.com/css/my.css" type="text/css" rel="stylesheet" />