在调用Android的从Java的WebView jQuery函数? [英] Call jQuery function in Android's WebView from Java?

查看:176
本文介绍了在调用Android的从Java的WebView jQuery函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打电话给其在HTML中定义JavaScript函数 像

I am trying to call javascript function which is defined in html like

WebView.loadUrl("javascript:hoge()");

我可以叫非jQuery的功能,但我不能说这是我所定义的'内$(文件)。就绪(函数(){})(jQuery的);功能,像波纹管

I can call non-jQuery function, but I can't call the function which I defined inside the '$(document).ready(function(){})(jQuery);', like bellow.

<script>
//I can call this function by webview.loadUrl("javascript:something()");
function something(){
    //do something
}

$(document).ready(function(){
    //but I can't call this function by webview.loadUrl("javascript:hoge()");
    function hoge(){
        //do something.
    }
})(jQuery);
</script>

有没有办法叫霍格()从Java像​​普通的javascript函数?

Is there a way to call hoge() from Java like ordinary javascript function?

我发现我可以用jQuery选择,即使该函数是不是里面的$(文件)。就绪(函数(){})(jQuery的);',但我也发现与解决方法,我可以' T选用额外的jQuery库。

I found that I can use jQuery Selector even if the function is not inside '$(document).ready(function(){})(jQuery);', but I also find that with that workaround, I can't use additional jQuery library.

belllow是实际code。

belllow is the actual code.

https://github.com/YoshimuraSei/AndrOutliner/tree/master/Outliner

这是HTML文件 的https://github.com/YoshimuraSei/AndrOutliner/blob/master/Outliner/assets/www/treeview.html

这是用java code,而我试图调用javascript函数。 <一href="https://github.com/YoshimuraSei/AndrOutliner/blob/master/Outliner/src/com/yslibrary/android/outliner/TreeViewFragment.java" rel="nofollow">https://github.com/YoshimuraSei/AndrOutliner/blob/master/Outliner/src/com/yslibrary/android/outliner/TreeViewFragment.java

and this is java code which I am trying to call javascript function. https://github.com/YoshimuraSei/AndrOutliner/blob/master/Outliner/src/com/yslibrary/android/outliner/TreeViewFragment.java

这是第100行,我想调用JavaScript function'test1()(见HTML文件的line34),它可以被称为,因为目前它不是内部'$(文件)。就绪(函数() {})(jQuery的);从测试1',但我不能把多余的jQuery库法nestedSortable()'()

on line 100, I am trying to call javascript function'test1()'(see line34 of the html file), and it can be called since currently it is not inside '$(document).ready(function(){})(jQuery);', but I can't call extra jQuery library method 'nestedSortable()' from test1().

我该如何解决这个问题?

How can I solve this?

编辑:
或者是装载jQuery库和插件这只是一个时间问题
我有负载的HTML的WebView后这些错误。


Or is this just a timing of loading jQuery library and plugins?
I got these error after load html to webview.

Uncaught TypeError: Cannot read property 'mouse' of undefined--From line 7 of file:///android_asset/www/js/jquery.ui.mouse.touch.js
Uncaught TypeError: Cannot read property 'sortable' of undefined--From line 15 of file:///android_asset/www/js/jquery.ui.nestedSortable.js

鼠标和排序是jquery.ui的性能,加载这两个文件之前应加载,所以我认为加载顺序是有点怪。 任何想法?

'mouse' and 'sortable' are properties of jquery.ui, which should be loaded before these 2 files are loaded, so I assume that Load order is little bit strange. any idea?

推荐答案

你可以做的是,当你调用web视图(如法=霍格)传递一个查​​询字符串参数。

What you can do is pass a query string parameter when you call the webview (eg. method=hoge).

然后在你的JavaScript可以检查参数是否存在等,并相应地运行霍格()方法

Then in your javascript you can check the existance of the param and run the hoge() method accordingly

不知道如果你有兴趣,但你也可以从JavaScript的使用了android的JavaScript接口调用你的Andr​​oid的Java code,看到的 http://developer.android.com/guide/webapps/webview.html

Not sure if your interested, but you can also call your android java code from javascript using the android javascript interface, see http://developer.android.com/guide/webapps/webview.html

这篇关于在调用Android的从Java的WebView jQuery函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆