JDK-8160323 : JavaFx Browser does not support window.onbeforeunload
  • Type: Bug
  • Component: javafx
  • Sub-Component: web
  • Affected Version: 8u92
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2016-06-23
  • Updated: 2017-07-18
  • Resolved: 2016-09-20
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_74"
Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Windows 7 64-bit

A DESCRIPTION OF THE PROBLEM :
JavaFxBrowser does NOT support window.onbeforeunload=true. In All other major browsers, doing this will ask the user whether they want to leave or not (they probably have unsaved changes). 

Since the applet will be no longer supported, you have suggested that sites use webstart to startup a JavaFx browser and then overlay the java ontop of the JavaFxBrowser. 
We can't do this successfully without having support for window.onbeforeunload=true since our HTML pages rely on that feature.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Try this jsfiddle, when attempt to leave the the page in all major browsers, you should be given some sort of confirm action dialog:

https://jsfiddle.net/zalun/yFuPr/

Here is the javascript to reproduce yourself on a JavaFxBrowser:
window.onbeforeunload = function(e) {
    e = e || window.event;
    e.preventDefault = true;
    e.cancelBubble = true;
    e.returnValue = 'test';
}

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
confimration dialog on attempt to renavigate browser
ACTUAL -
no confirmation dialog, unsaved changes will be lost

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
https://jsfiddle.net/zalun/yFuPr/

Here is the javascript to reproduce yourself on a JavaFxBrowser:
window.onbeforeunload = function(e) {
    e = e || window.event;
    e.preventDefault = true;
    e.cancelBubble = true;
    e.returnValue = 'test';
}
---------- END SOURCE ----------


Comments
Our customer has attempted the workaround suggestions in JDK-8160749 however what is missing from both suggestions is the Javascript side of the workaround. The Java side was pretty easy and straightforward to implement, but our customer can���t find a place to tie into on the Javascript side since the beforeunload event doesn���t get fired. The unload event is too late as we need to make async calls or allow the user to remain on the page. Can you offer a little insight into what our customer would use to replace the ���window.onbeforeunload��� event as is suggested in both workarounds?
18-07-2017

JavaFX webView's web browser engine is webKit which is open source web browser engine. The execution of JavaScripts called from JavaFX webView should be interpreted by webKit's javaScript interpreter. So, perhaps, webKit's javaScript interpreter does not support windows.onbeforeunload correctly.
04-07-2016

According to the reported bug, there is no class or component in JavaFX called "JavaFxBrowser". So, could you please confirm us whether you are using the JavaFX "WebView" component or a third party component that just happens to be written in JavaFX.
27-06-2016