Issue
<!doctype html>
<head>
<meta charset="utf-8">
<meta name="author" content="Paul Lewis" />
<meta name="viewport" content="width=device-width">
<title>NEW ONE</title>
<style>
html, body {
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<div id="container"></div>
<script type="text/javascript" src="three.js-master/build/three.js"></script>
<!-- <script src="threejs/js/JSONLoader.js"></script>
-->
<script type="module" src="three.js-master/src/loaders/ObjectLoader.js"></script>
<script type="module" src="three.js-master/src/helpers/GridHelper.js"></script>
<script type="module" src="three.js-master/src/loaders/JSONLoader.js"></script>
<script type="text/javascript" src="three.js-master/examples/js/controls/OrbitControls.js"></script>
<script type="text/javascript">
function init()
{
function myFunction(data1,data2)
{
//Do things
}
}
window.onload = init;
</script>
</body>
</html>
I have displayed the web page in Web View in Android. Now I want to periodically pass data to the myFunction() js function located in the html page. I have enabled javascript in the webView and after calling webView.loadUrl("http://XXXX/index.html");
, I am calling
js_interface=new WebInterface(this,webView);
webView.addJavascriptInterface(js_interface,"Android");
,to load the Interface class used to interact with the Javascript functions.
WebInterface.java:
public class WebModelInterface {
Context mContext;
WebView webView;
/** Instantiate the interface and set the context */
WebModelInterface(Context c, WebView webView) {
mContext = c;
this.webView=webView;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
Log.e("WebInterface","Called by JavaScript Code!!!");
}
public void setTarget(int finalI,double value){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript("javascript:myFunction("+data1+","+data2+");",null);
}
else
webView.loadUrl("javascript:setJointPosition("+finalI+","+value+");");
}
}
I get the error: Uncaught ReferenceError: setJointPosition is not defined
I have also tried to call the function from the chrome console which gives the same error. On calling the function from within the script, it executes fine. But after loading the page and calling it gives error. Please help. Thanks!
Solution
Solved it! Made a stupid mistake of not noticing that the JavaScript code was enclosed in the init() function. Was trying to call the myFunction directly, which didn't exist outside init(). So, I removed the init() function and was able to call the myFunction properly from the Android Class.
Answered By - Rhnbmpl
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.