ReferenceError:" alert"没有定义 [英] ReferenceError: "alert" is not defined

查看:524
本文介绍了ReferenceError:" alert"没有定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从java代码调用java脚本函数。

I am trying to call a java script function from java code.

这是我的Java代码

    public static void main(String[] args) throws FileNotFoundException {
    try {
        /**
         * To call a anonymous function from java script file
         */
        ScriptEngine engine = new ScriptEngineManager()
                .getEngineByName("javascript");
        FileReader fr = new FileReader("src/js/MySpec.js");
        engine.eval(fr);

    } catch (ScriptException scrEx) {
        scrEx.printStackTrace();
    }
}

这是我的java脚本文件:

Here is my java script file:

(function() {
  alert("Hello World !!!");
})();

但是当我运行驱动程序类的main方法时,它给出了如下错误:

But when I run main method of driver class it is giving me error as below:

Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "alert" is not defined. (<Unknown source>#2) in <Unknown source> at line number 2
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:232)
at Java6RhinoRunner.load(Java6RhinoRunner.java:42)
at Java6RhinoRunner.main(Java6RhinoRunner.java:12)

我所知道的是它需要一些脚本引擎来执行它。

What I know is that it need some script engine to execute it.

为此,我将rhino.jar文件添加到我的类路径中。但是这个不工作。

For that I added rhino.jar file in to my class path.But this is not working.

我没有得到如何解决这个错误。
请帮忙。谢谢。

I an not getting how to solve this error. Please help.Thanks in advance.

推荐答案

alert 不是JavaScript的一部分,它是Web浏览器提供的窗口对象的一部分。所以它在您尝试使用它的上下文中不存在。(对于 setInterval setTimeout ,和其他与计时器相关的东西,仅供参考。)

alert is not part of JavaScript, it's part of the window object provided by web browsers. So it doesn't exist in the context you're trying to use it in. (This is also true of setInterval, setTimeout, and other timer-related stuff, FYI.)

如果您只是想做简单的控制台输出,Rhino提供 print 函数到你的脚本,所以你可以用 print 替换 alert 。您的脚本也可以访问所有Java类等,例如 java.lang.System.out.println('Hello'); 可以使用您的JavaScript脚本(虽然它提供的 print 函数有点多余)。您还可以通过 ScriptEngine.put 轻松地为脚本提供Java变量,例如:

If you just want to do simple console output, Rhino provides a print function to your script, so you could replace alert with print. Your script also has access to all of the Java classes and such, so for instance java.lang.System.out.println('Hello'); would work from your JavaScript script (although it's a bit redundant with the provided print function). You can also make Java variables available to your script easily via ScriptEngine.put, e.g:

engine.put("out", System.out);

...然后在你的剧本中:

...and then in your script:

out.println('Hello from JavaScript');

...所以这是从脚本输出的第三种方式。 : - )

...so that's a third way to do output from the script. :-)

参见 javax.script 包文档,特别是 ScriptEngine #put ,或者对于更复杂的情况, Bindings (以及 SimpleBindings )和 ScriptContext

See the discussion in the javax.script package documentation, in particular ScriptEngine#put, or for more complex cases, Bindings (and SimpleBindings) and ScriptContext.

这篇关于ReferenceError:&quot; alert&quot;没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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