JavaFX shows some odd behaviour when loading a page that has an identical url to a page that has been loaded before. The code below demonstrates this issue:
After class initalization a page is loaded, then a highlight is applied to an arbitrary html element using a custom style class. This highlight is rendered correctly.
Finally, through user input event, the WebView is told to load a new page (with the same URI). Instead of showing the page as-is, the highlight is shown as well.
WebView webView = new WebView();
static String URI = "http://www.example.com";
public void loadPage() {
// Step 1: load page
webView.getEngine().load(URI);
// Step 2: Change style attribute in page
(Element) element = xpath.evaluate("//div[@id='mydiv']", webView.getEngine().getDocument(), XPathConstants.NODE);
element.setAttribute("class", "mystyle");
}
handle() {
// Step 3: load page again
webView.getEngine().load(URI);
}
I have experimented with forcing the page to reload with WebView.getEngine().reload(), disabling cache, waiting for workers to complete, etc.
The only option I currently see is to create a new instance of the WebView, but as this is pretty CPU heavy, I prefer to reuse the object rather than creating it new every time I want to revert to the original page.