javafx webview not supporting Ajax web features


I am trying to open webpage in webview using JavaFx . Its opening the web page properly but its not supporting the Ajax based web features like partial refreshing and new window popup handling I am using the following code

  final Group group= new Group();
  Scene scene= new Scene(group);
  WebView  webview = new WebView ();

  eng= webview.getEngine();


            String url="";
            new Callback<PopupFeatures, WebEngine>() {
            public WebEngine call(PopupFeatures config) {
            smallView = new WebView();

            ChatPopup frm = new ChatPopup(smallView);
            return smallView.getEngine();


  catch(Exception ex){}

5/6/2013 4:44:47 AM

Accepted Answer

WebView does support Ajax.

  1. Run the following app.
  2. Click on the "Load data from server into div" button.
  3. Page will be refreshed with data fetched from the server.


import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class WebViewAjax extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(Stage stage) {
    WebView webView = new WebView();

    final Scene scene = new Scene(webView);
5/6/2013 7:39:04 PM

If you need to make AJAX calls to cross-site services in the WebView, you can get around the security restrictions by making your AJAX calls through an up-call to Java. For example you could write or find a class with a ".request()" method that takes a JSObject as a parameter (the same JSObject format that jQuery's $.ajax() method takes, preferably), and inject a Java object that will expose that method:

WebView myWebView; //assuming it's initialized and points to an actual WebView

WebEngine engine = myWebView.getEngine();
JSObject window = null;
    window = (JSObject) engine.executeScript("window");
}catch (JSException e){
if (window != null){
    window.setMember("myAjax", new AJAXProxyClass());

You can also directly override jQuery's ajax method with your own upcalling method, so that the difference is completely transparent to the javascript code, i.e.:

engine.executeScript("$.ajax = new function (o) { myAjax.request(o); };");
engine.executeScript("_$ = window.$");

This will replace jQuery's "$.ajax" call with the one from your Java object seamlessly. (I set the "_$" variable because jQuery will overwrite $ with it sometimes if it detects conflicts, returning jQuery to its original version.) This means that in most cases, any javascript code will not have to care whether it is running in your WebView or not.

A warning, though - jQuery's ajax call is rather complex, and this may break some of the jQuery ajax extensions if you're not careful about how you handle it. It should work just fine for the most common kinds of GET and POST calls, though.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow