2

I'm trying to call JavaScript in a JavaFx WebView from Java, but I get:

Exception in thread "JavaFX Application Thread" netscape.javascript.JSException: TypeError: undefined is not a function
at com.sun.webkit.dom.JSObject.fwkMakeException(JSObject.java:128)
at com.sun.webkit.WebPage.twkExecuteScript(Native Method)
at com.sun.webkit.WebPage.executeScript(WebPage.java:1439)
at javafx.scene.web.WebEngine.executeScript(WebEngine.java:982)

.java file

private WebView emailSubject() {
    String pageURL = "D:myproject\\src\\resources\\WEB_INF\\forms\\readMail\\emailBody.html";
    pageURL = pageURL.replace("\\", "/");
    webView = new WebView();
    webView.setMaxHeight(52);

    webEngine = webView.getEngine();
    emailSubject = getHTMLMailSubject();

    webEngine.getLoadWorker().stateProperty().addListener((ov, oldState, newState) -> {
        if (newState == State.SUCCEEDED) {
            webEngine.executeScript("testCheckMate(\"" + emailSubject + "\");");
        }
    });

    webEngine.load("file:" + pageURL);
    return webView;
}

Exception which points to this line:

webEngine.executeScript("testCheckMate(\"" + emailSubject + "\");");

The HTML:

<!-- Latest compiled and minified JavaScript -->
<script src="../../baseJS/JQuery/jquery-1.11.3.js"></script>
<script src="../../baseJS/JQuery/jquery.min.js"></script>

<div class="panel panel-success">
    <div class="panel-heading">
        <h3 class="panel-title">Home Alone</h3>
    </div>
</div>

<script>
    $(document).ready(function() {
        window.testCheckMate = function (data) {
            $(".panel-title" ).append(data);
        };
    });     
</script>

What am I doing wrong? Thank you all in advance.

Please note that I've tried:

load(); already

$(".panel-title" ).load(data);

as well as with

testCheckMate outside $(document).ready(function(), but still nothing.

testCheckMate = function (data) {
    $(".panel-title" ).load(data);
};
Drew Gaynor
  • 8,292
  • 5
  • 40
  • 53
Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142
  • Is this working: webEngine.executeScript("testCheckMate('test_value');"); – Uluk Biy Nov 09 '15 at 15:26
  • Change this: `window.testCheckMate ` by this: `function testCheckMate ` – Hackerman Nov 09 '15 at 15:28
  • @UlukBiy Biy No. Still `netscape.javascript.JSException: TypeError: undefined is not a function` – Program-Me-Rev Nov 09 '15 at 15:29
  • @Hackerman Still `netscape.javascript.JSException: TypeError: undefined is not a function` – Program-Me-Rev Nov 09 '15 at 15:35
  • 1
    I'm guessing here but I think there is probably some timing issue between the various stages of the document being loaded/ready. You define the method only when the document is "ready" (Javascript), and try to invoke it when the document is "loaded" (from JavaFX). It's not clear (to me, anyway) which of those happen first, and if the jquery library is loaded at the time. Is there any way you can rewrite the example as a [MCVE]? – James_D Nov 09 '15 at 18:01
  • Hi @James_D. I've narrowed in on the problem. The problem was that I was trying to add multiple WebViews on the same WebView at the same time. The problem has been mentioned [here: Cannot execute JavaScript when multiple WebViews are used at the same time, JDKJDK-8129398](https://bugs.openjdk.java.net/browse/JDK-8129398) as a bug. Please take a look at [this: How to add multiple html pages to the same WebView](http://stackoverflow.com/questions/33619437/how-to-add-multiple-html-pages-to-the-same-webview) and see if you can help in any way. Thank you in advance. – Program-Me-Rev Nov 09 '15 at 22:32

0 Answers0