Issue
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?
Solution
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 accordingly
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
Answered By - Marty
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.