问题
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
usingwebView.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?
回答1:
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()
来源:https://stackoverflow.com/questions/32429069/loading-local-javascript-file-in-webview-in-java-8u60