JavaFX8 WebEngine的executeScript(“window”)方法是否引用JavaScript窗口对象? [英] Does JavaFX8 WebEngine's executeScript("window") method refer to the JavaScript window object?

查看:1950
本文介绍了JavaFX8 WebEngine的executeScript(“window”)方法是否引用JavaScript窗口对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我正在使用的代码片段:

I'm wondering about this code snippet I'm using:

WebView webView = new WebView();
JSObject jsobj;
webEngine = webView.getEngine();
try {
    webEngine.load(getClass().getResource("index.html").toExternalForm());
} catch (Exception e) {
    e.printStackTrace();
}
try {
    jsobj = (JSObject) webEngine.executeScript("window");
    jsobj.setMember("java", new DataModel());
} catch (Exception e) {
    e.printStackTrace()
}

这一行 webEngine.load(getClass()。getResource(index.html)。toExternalForm()); 加载我的索引.html 进入 WebView 。由于它是一个单页应用程序,这就是我所需要的,而且没有更多的问题。


现在回到标题中的问题: < br>
这一行: jsobj =(JSObject)webEngine.executeScript(window); 设置 JSObject 参考由w3school定义的窗口对象
意味着 jsobj 现在等于窗口对象,表示浏览器中的打开窗口


如果是这样的话:如果我在 WebView 的上下文菜单中使用重新加载页面,这可能是我的应用程序无法工作的原因,因为它会生成一个新的窗口对象


奖金问题:在我的JavaScript中,我可以使用 java.methodName()访问模型; 是java,这里是标识符或我如何调用它?它不完全是实例,所以我不会这样称呼它。



编辑:
这是我的Java类:

This line webEngine.load(getClass().getResource("index.html").toExternalForm()); loads my index.html into the WebView. Since it is a single page application, that's all I need and there are no more questions about it.

Now to get back to the question in the title:
Does this line: jsobj = (JSObject) webEngine.executeScript("window"); set the JSObject reference to the "window object" as it's defined by w3school?
Meaning that jsobj is now equal to the window object, that represents the open window in the browser?

If so: Is this likely to be the reason why my application won't work if I use "reload page" over the context menu in the WebView, since it would generate a new window object?

Bonus question: In my JavaScript I can acces the model using java.methodName(); is "java" here an identifier or how would I call it? It's not exactly an instance, so I wouldn't call it like this.

This is my Java class:

public class DataModel{
    public void alert(String msg) {
        System.out.println(msg);
    }
}

来自我的JavaScript,嵌入在<$ c中$ c> index.html 我可以调用 java.alert(hello world); 打印 hello world 。由于我将模型引用传递给窗口对象我的JavaScript中的每个函数都可以访问 DataModel.class 。从JavaScript的角度来看:是java。标识符,引用,实例,......?

From my JavaScript, which is embedded in index.html I can call java.alert("hello world"); to print hello world. Since I'm passing the model reference to the window object every function in my JavaScript has access to the DataModel.class. From the JavaScript point of view: Is "java." an identifier, reference, instance, ...?

推荐答案

根据此站点 webEngine.executeScript(window) 实际上返回JavaScript Window对象。还有一大堆其他代码片段好看的东西!

According to this site, webEngine.executeScript("window") would in fact return the JavaScript Window object. There's also a whole bunch of other code snippet goodies to look!

java 在这种情况下是一个可以处理的句柄由您的JavaScript在Window对象中使用。因此,当引用 java 时,您的JavaScript可以访问 DataModel 对象。

java in this case is a handler that can be used by your JavaScript, within the Window object. So your JavaScript, when referencing java can expect to access the DataModel object.

此外,您通常希望在JavaScript中使用 location.reload()刷新页面。

Also, you normally want to refresh your page with location.reload() in JavaScript.

如果您希望在重新加载之间保存状态,则需要捕获当前的HTML,然后使用WebView的 loadContent功能。您还可以获取WebView的文档,以便可以直接修改任何级别的节点。

If you're looking to save state between reloads, you'll want to capture the current HTML and then use the WebView's loadContent functionality when the reload is complete. You could also get the WebView's document so you can directly modify nodes at any level.

这篇关于JavaFX8 WebEngine的executeScript(“window”)方法是否引用JavaScript窗口对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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