Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading local JavaScript file in WebView in Java 8u60

Until Java 8u51, the following worked fine:

  • Have an HTML file containing <script src="javascript.js"></script>
  • Load this HTML file into a WebView
  • Call a function contained in javascript.js using webView.getEngine().executeScript()

After switching to 8u60 however, it no longer works:

Exception in thread "JavaFX Application Thread" netscape.javascript.JSException: TypeError: undefined is not a function

So the file javascript.js is no longer loaded. It works when started from an IDE, where JavaScript file just lies on the file system. However, it no longer works when the application is packaged and therefore javascript.js is inside a JAR file.

Any idea what has changed and how this can be fixed?

like image 315
Michel Jung Avatar asked Sep 06 '15 22:09

Michel Jung


1 Answers

Since Java 8u60, local javascript files referenced in html files are not executed anymore in the WebView. When running the program with an older version, it still works. I found no information that this change in behaviour was intended by Oracle.

As a workaround you can either place the javascript code directly into the HTML file, or load in java the contents of the javascript file into a String and execute it with webView.getEngine().executeScript()

like image 58
gogotox Avatar answered Nov 07 '22 16:11

gogotox