“document” is not defined in <eval> java ScriptEngine

情到浓时终转凉″ 提交于 2019-12-12 02:55:34

问题


i'm trying to call document in java using scriptEngine here is my code

     ScriptEngineManager scriptEngineManager=new ScriptEngineManager();
     ScriptEngine scriptEngine=scriptEngineManager.getEngineByName("nashorn");
    scriptEngine.eval("function func(){" +
    "document.location.href=someUrl;" +
                  "var text=document.getElementsByTagName('textarea')[0];"}"
                    );
            Invocable invocable= (Invocable) scriptEngine;
            ((Invocable) scriptEngine).invokeFunction("func");

it throws an exception

javax.script.ScriptException: ReferenceError: "document" is not defined in <eval> at line number 1

回答1:


The script engine in Java provides the ability to execute Javascript code, but does not provide an emulation of a browser.

document, window and other objects are defined by the browsers but are not mandated to be present by ECMAScript specification (http://www.ecma-international.org/ecma-262/5.1/#sec-15).



来源:https://stackoverflow.com/questions/35744204/document-is-not-defined-in-eval-java-scriptengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!