1

I'm trying to insert a String of text into a <h3 class="panel-title"></h3> element inside a HTML document inside a WebView.

.java

webEngine = webView.getEngine();

String headerText = "This is the Header";
webEngine.executeScript("testCheckMate(" + headerText + ");");

the HTML

<h3 class="panel-title"></h3>

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

I, however, keep getting the error:

Caused by: netscape.javascript.JSException: SyntaxError: Unexpected keyword 'this'. Expected ')' to end a argument list.
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)

What am I doing wrong?

Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142
  • 2
    You're missing quotation marks...your are executing the javascript `testCheckMate(This is the Header)` instead of `testCheckMate("This is the Header")` – Steve Clanton Nov 07 '15 at 19:23

2 Answers2

1

The string you pass to the Javascript method needs to have quotes:

webEngine.executeScript("testCheckMate(\"" + headerText + "\");");
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Hi @James_D . Could you please take a look at this similar question I posted here and see if you can help, [Error calling JavaScript from Java](http://stackoverflow.com/questions/33612267/error-calling-javascript-from-java)? – Program-Me-Rev Nov 09 '15 at 17:49
  • Your answer helped a lot with a research I was doing relating to calling JavaScript functions from Java. But, I needed to post a follow-up question, which your answer was not really inclusive of. It would seem like the question already had an answer elsewhere. – Program-Me-Rev Nov 09 '15 at 17:59
  • By using this my String got changed from **C:\Users\ to C:Users** Slash got disappeared anyone knows why? – Dinesh Falwadiya Feb 14 '18 at 12:45
0

You pass a string which already contains the " character. So from Java side you must quote this with \" . However in JavaScript this String is again parsed and the \" will be the end of string. This is why the end argument list error is thrown. When passing strings, which are used to invoke functions, you should use on Java side:

ret = ret.replaceAll("\"","\\\\\""); 

This solved my problem.

Ashok kumar Ganesan
  • 1,098
  • 5
  • 20
  • 48