使用J2V8从.js执行函数 [英] execute function from .js with J2V8
问题描述
我正在使用J2V8在Android上执行JavaScript代码。在我的Java代码中,我可以访问和执行单独的.js文件的JavaScript函数吗?如果有可能,我该怎么做?
I am using J2V8 for execute JavaScript code on Android. In my Java code, can I access and execute JavaScript functions of a separate .js file? If it is possible, how can I do that?
谢谢:
Bernat
推荐答案
与许多JavaScript环境一样,您只需加载包含您希望执行的其他功能的脚本浏览器示例。现在可以使用添加到全局范围的任何函数:
Like with many JavaScript environments, you simply load the script that contains the other functions you wish to execute browser example. Any functions that are added to the global scope, are now available to you:
V8 v8 = V8.createV8Runtime();
v8.executeScript(readFileAsString(script1)); //包含函数foo();
v8.executeScript(readFileAsString(script2)); //包含函数bar(x,y);
v8.executeJSFunction(foo);
v8.executeJSFunction(bar,7,8);
这篇关于使用J2V8从.js执行函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!