JDK-8160749 : WebView does not support window.onbeforeunload
  • Type: Enhancement
  • Component: javafx
  • Sub-Component: web
  • Affected Version: 8u66,9
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_8
  • CPU: x86
  • Submitted: 2016-07-04
  • Updated: 2019-03-23
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Duplicate :  
Relates :  
Description
JavaFx WebView 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,  we ���Oracle)  have suggested that sites use webstart to startup a WebView and overlay the java ontop.
 
Our suctomers 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 in a WebView, when attempting to leave the the page in all major browsers, you will be given some sort of confirm action dialog:

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

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

Expected Behavior:
confirmation dialog on attempt to re-navigate browser
ACTUAL -
no confirmation dialog, unsaved changes will be lost 

Reproducing test program is attached.
Comments
So, you suggested us two methods to solve the unsupported/unimplemented callback or dialogue of " windows.onbeforeunload". The method1 (WebEngine.promptHandler Callback) should be implemented as below: (1) In customer/user's Java application program, promptHandler Callback should be set by calling WebEngine.setPromptHandler(). And the promptHandler call back should launch a dialogue window as done in http://stackoverflow.com/questions/18817269/javascript-in-javafx-webview for confirmationWindow. (2) In client/customer's java script program, replacing "window.onbeforeunload" event with prompt event. Is that right? The method2 (Bind custom JSObject to the WebEngine) should be implemented as below: (1) In customer/user's Java application program, as done in your https://gist.github.com/ToddG/2609738, // extend the webview js context webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() { @Override public void changed(final ObservableValue<? extends Worker.State> observableValue, final State oldState, final State newState) { if (newState == State.SUCCEEDED) { JSObject win = (JSObject) webEngine.executeScript("window"); webEngine.executeScript("confirmationWindow"); } }); (2) In client/customer's java script program, replacing window.onbeforeunload event processing with "confirmationWindow" java script. Is that right? Please elaborate the above two methods' implementations in detail, especially for java script part.
25-07-2016

WebView as a embedded browser engine provides call back instead of Modal dialogue for javascript events such as alert, prompt etc... Currently for "window.onbeforeunload" call back or dialogue is not implemented. Alternatively you can use (Any of these, and it requires modification in the client JavaScript code to include prompt while navigating to new page) 1. WebEngine.promptHandler Callback 2. Bind custom JSObject to the WebEngine after SUCCEDDED ( in webEngine.getLoadWorker().stateProperty().addListener) with a custom class.
08-07-2016