从 Java 调用 Android WebView 中的 jQuery 函数?
我正在尝试调用在 html 中定义的 javascript 函数 就像
WebView.loadUrl("javascript:hoge()");
我可以调用非 jQuery 函数,但我无法调用我在 '$(document).ready(function(){})(jQuery);' 中定义的函数,如下所示。
<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>
有没有办法像普通的 JavaScript 函数一样从 Java 调用hoge()?
我发现即使该函数不在 '$(document).ready(function(){})(jQuery);' 内,我也可以使用 jQuery 选择器,但我还发现,通过该解决方法,我无法使用额外的jQuery 库。
Bellow是实际的代码。
https://github.com/YoshimuraSei/AndrOutliner/tree/master/Outliner
这是 html 文件 https://github.com/YoshimuraSei/AndrOutliner/blob /master/Outliner/assets/www/treeview.html
这是我试图调用javascript函数的java代码。 https://github.com /YoshimuraSei/AndrOutliner/blob/master/Outliner/src/com/yslibrary/android/outliner/TreeViewFragment.java
上线100,我正在尝试调用javascript函数'test1()'(参见html文件的第34行),并且可以调用它,因为当前它不在'$(document).ready(function(){})(jQuery );',但我无法从 test1() 调用额外的 jQuery 库方法“nestedSortable()”。
我该如何解决这个问题?
编辑:
或者这只是加载 jQuery 库和插件的时间?
将 html 加载到 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
'mouse' 和 'sortable' 是 jquery.ui 的属性,应该在加载这两个文件之前加载,所以我认为加载顺序有点奇怪。 有什么想法吗?
I am trying to call javascript function which is defined in html
like
WebView.loadUrl("javascript:hoge()");
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>
Is there a way to call hoge() from Java like ordinary javascript function?
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 is the actual code.
https://github.com/YoshimuraSei/AndrOutliner/tree/master/Outliner
and this is html file
https://github.com/YoshimuraSei/AndrOutliner/blob/master/Outliner/assets/www/treeview.html
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
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?
Edit:
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
'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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我目前是一名 JavaScript 工程师。从这次经历中,我现在知道出了什么问题。
这是一个范围和范围。与时序相关的问题。
$(document).ready(function(){});
中的函数无法从外部调用,因为它们不是全局的。如果你想从外部调用它们,你必须将其设置为全局。并且您必须等到 javascript 完全加载并准备好使用。I'm currently working as a JavaScript engineer. From this experience, now I can see what was wrong.
It's a scope & timing related issue.
Functions inside the
$(document).ready(function(){});
cannot be called from outside, since they are not global. if you want to call them from outside, you have to make it global. And you have to wait until javascript is fully loaded and ready to be used.您可以做的是在调用 webview 时传递查询字符串参数(例如 method=hoge)。
然后在你的javascript中你可以检查参数是否存在并相应地运行
hoge()
方法不确定你是否感兴趣,但你也可以使用android javascript接口从javascript调用你的android java代码,请参阅http://developer.android.com/guide/webapps/webview.html
What you can do is pass a query string parameter when you call the webview (eg. method=hoge).
Then in your javascript you can check the existance of the param and run the
hoge()
method accordinglyNot 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
好吧,你总是可以使用 webview 的 loadUrl 方法调用 javascript 函数;
well, you can always call a javascript function using loadUrl methode of your webview;
只需在您的 html 文件中添加一个 jquery 函数并命名为例如 show_main_pop :
并在 android studio 中调用此函数,如下所示:
Just add to your html file a jquery function and named for example show_main_pop :
and in android studio call this function like that: