首页 > 解决方案 > 你怎么知道一个webview有一些JS功能?

问题描述

我们知道Java可以通过'webView.getEngine().executeScript("setServerStatus('xxxxx')")'的形式调用javafx webview中的js函数,但是有时候切换HTML页面时却找不到js函数。在后台,我使用线程实时更新HTML页面的网络状态,它有时会报告以下异常:

Exception in thread "JavaFX Application Thread" netscape.javascript.JSException: ReferenceError: Can't find variable: setServerStatus
at com.sun.webkit.dom.JSObject.fwkMakeException(JSObject.java:146)
at com.sun.webkit.WebPage.twkExecuteScript(Native Method)
at com.sun.webkit.WebPage.executeScript(WebPage.java:1509)
at javafx.scene.web.WebEngine.executeScript(WebEngine.java:1005)
at com.gzhuiqun.adapter.Application.lambda$4(Application.java:370)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:748)

Java是否提供了判断当前HTML中是否存在js函数的方法?或者更优雅的方式来做到这一点?在webview html5中,正常的js监控网络状态功能无效。

标签: javajavafxjavafx-8javafx-webengine

解决方案


推荐阅读