我可以创建一个“窗口”吗? 在 Java6 Rhino 脚本引擎中运行的 javascript 对象
- 我想在我的 Java6 服务器上运行一些 Javascript - 即使用 javax.script API,特别是 Rhino 脚本引擎。 (尽管另一个解决方案是可以接受的)
- 脚本文件被创建并创建。 由第三方支持,所以我不想下载它并编辑它,以防它随着时间的推移而改变。
- 该脚本直接引用“窗口”对象(可能还有“文档”对象等),Rhino
似乎不支持该对象。
我可以这样做吗?如果可以,怎么做?
- 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 acceptable)
- The script file is created & supported by a third party, so I don't want to download it and edit it in case it changes over time.
- The script directly references the 'window' object ( and probably the 'document' object etc. ) which Rhino does not
seem tosupport.
Can I do this, and if so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
...当然,除非您使用脚本尝试访问的属性填充它们,否则它们不会做很多好事。
您不能只用标准浏览器 API 填充它们 - 其中大多数在浏览器上下文之外没有任何意义。
... 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.
窗口和文档对象仅由网络浏览器提供,而不是 ECMAScript 标准。 它们允许脚本访问当前浏览器窗口和 HTML 文档。 文档对象实际上是 W3C DOM 的实现。
Rhino 是 ECMAScript/JavaScript 1.7 的纯粹实现,因此一般不了解 HTML 页面、窗口和浏览器内容。 它是一种通用脚本语言,恰好大部分嵌入到 Web 浏览器中,因此您通常可以使用浏览器提供的全局对象。
您当然可以定义一些名为“window”和“document”的全局可访问对象,它们只是不执行任何操作的存根,但是您要执行的脚本可能使用它们的一些方法和/或属性,因此这不会对你有很大帮助。 如果您想执行一个为在浏览器环境中执行而编写的脚本,您需要提供一个完整的类似“浏览器”的环境。
如果这是可能的并且在服务器上下文中有意义是另一个问题......
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...