Can I create a 'window' object for javascript running in the Java6 Rhino Script Engine

后端 未结 2 1488
悲哀的现实
悲哀的现实 2021-01-05 17:01
  • I want to run some Javascript on my Java6 server - i.e. using the javax.script API, specifically the Rhino Script Engine. (Although another solution would be acceptabl
相关标签:
2条回答
  • 2021-01-05 17:13

    The window and document objects are just provided by web-browsers and are not part of the ECMAScript standard which Rhino implements. They are there to allow a script to access the current browser window and the HTML document. The document object is actually an implementation of the W3C DOM.

    Rhino is a pure implementation of ECMAScript/JavaScript 1.7 and therefore does not know anything about HTML pages, windows and browserstuff in general. It is a general purpose scripting language which just happens to be mostly embedded into a web browser and thus you can usually use the global objects provided by the browser.

    You can of course define some globally accessible objects with the names "window" and "document" which are just stubs which do nothing, but the script you want to execute probably uses some methods and/or properties on them, so this won't help you much. If you want to execute a script, which was written for execution in a browser environment, you need to provide a full "browser"-like environment.

    If that is possible and makes sense in a server context is another question...

    0 讨论(0)
  • 2021-01-05 17:30
    var window = {}
    var document = {}
    

    ... of course, they won't do a lot of good unless you populate them with the properties that the script is trying to access.

    You can't just populate them with the standard browser APIs - most of them don't make sense outside the context of the browser.

    0 讨论(0)
提交回复
热议问题