我可以为Java6 Rhino脚本引擎中运行的javascript创建一个"window"对象吗? [英] Can I create a 'window' object for javascript running in the Java6 Rhino Script Engine

查看:169
本文介绍了我可以为Java6 Rhino脚本引擎中运行的javascript创建一个"window"对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我想在Java6服务器上运行一些Javascript,即使用javax.script API,特别是Rhino Script Engine. (尽管可以接受其他解决方案)
  • 脚本文件已创建&受第三方支持,因此我不想下载并对其进行编辑,以防它随时间变化.
  • 该脚本直接引用Rhino不支持的窗口"对象(可能还有文档"对象等).

我可以这样做吗?如果可以,怎么办?

Can I do this, and if so, how?

推荐答案

窗口和文档对象仅由网络浏览器提供,不属于 W3C DOM 的实现.

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是ECMAScript/JavaScript 1.7的纯实现,因此通常对HTML页面,窗口和浏览器内容一无所知.它是一种通用脚本语言,恰好恰好嵌入在Web浏览器中,因此您通常可以使用浏览器提供的全局对象.

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.

您当然可以定义一些名称为"window"和"document"的全局可访问对象,它们只是不执行任何操作的存根,但是您要执行的脚本可能在其上使用了一些方法和/或属性,因此不会对您有多大帮助.如果要执行为在浏览器环境中执行而编写的脚本,则需要提供完整的类似于浏览器"的环境.

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...

这篇关于我可以为Java6 Rhino脚本引擎中运行的javascript创建一个"window"对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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