android jquery android-webview android-2.2-froyo

android - JQuery no funciona en WebView



android-webview android-2.2-froyo (6)

Intenté incluir archivos JQuery en los activos / scripts y en Internet, pero el cuadro de diálogo de alerta no aparece. Obtuve el registro de salida y lo hago output.html, funciona en Windows (¡qué extraño!). ¿Cuál es el problema con 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); }

Esta es la salida del registro después de agregar la extensión ".html". Funciona en Firefox pero no en 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>


Como ya se mencionó en Android, WebView no carga jQuery :

¿Dónde se encuentra el script scripts / jquery-1.6.2.min.js? Si se encuentra en el directorio de activos, debe inicializar webView dándole el directorio de activos como baseUrl:

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

o

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

Puedes intentar crear un archivo .js simple con una función simple como

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

e intente acceder a la función de relleno en su html para comprobar si el archivo .js está incluido.


Deberías dar una ruta completa para cargar javascript y css, por ejemplo.

<link href="http://yourdomain.com/css/my.css" type="text/css" rel="stylesheet" />


Necesitará tener el archivo jquery.js en su carpeta de activos / scripts para que esto funcione.

scripts / jquery-1.6.2.min.js

Eso debería funcionar.


Supongo que algunas funciones de javascript como aviso y alerta (ventanas emergentes del sistema) deben implementarse mediante su código. Una guía sencilla ..,

1. Crea una clase 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. En su programa agregue , webView.addJavascriptInterface(new Jscalls(this), "Android");

3. En las páginas html , en lugar de alert("hello") , use Android.alert("hello")

Espero que funcione :)


setPluginsEnabled en la configuración de WebView en true .


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

ser cambio a

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

para obtener el archivo de activos, deberá acceder a la ruta de activos de la aplicación. Una aplicación es un usuario en Android, por lo que no se puede acceder a la ruta comience con el directorio "file: //".