Does JavaFX web viewer support loading a web page from a string of html code? My code is currently functional under scenario 1 below. However, I will need to break the webFile up into two pieces (top & bot), and then insert a string of html in between. The final result is loaded via webviewer. Please see #2 for my intent (it doesn't work). Can anyone suggest on how I might be able to pull this off? Thanks!
1.
String webFileStr = (new File(webFile)).toURI().toURL().toString();
webEngine.load(webFileStr);
2.
String webStr = topSlice + data + botSlice;
webEngine.load(webStr);
Use WebEngine.loadContent.
webView.getEngine().loadContent("<html>hello, world</html>", "text/html");
Javadoc description:
Loads the given content directly. This method is useful when you have content composed in memory, or loaded from some system which cannot be reached via a URL (for example, the SVG text may have come from a database). As with load(String), this method is asynchronous. This method also allows you to specify the content type of the string being loaded, and so may optionally support other types besides just HTML.
As Hiux suggests in comments:
using a
<base>
tag is the trick to load relative resources withloadContent(String)
as demonstrated in his related answer to How to load both html and javascript into webengine from loadContent()?.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With