This page describes how to work with pop-ups, display or suppress them.

Overview

Any web page can display pop-up windows using one of the following ways:

  1. Using the window.open() JavaScript function. For example:

     window.open("https://www.google.com", "_blank", "resizable=yes,
         top=500, left=500, width=400, height=400");
    
  2. Through a link with the target attribute:

     <a href="https://www.google.com" target="_blank">Open Google</a>
    

By default all pop-ups are suppressed.

Opening Pop-ups

In order to change the default behavior and take control over creation of pop-ups use the CanOpenPopupCallback and OpenPopupCallback callbacks.

The CanOpenPopupCallback callback is invoked when the engine wants to know whether pop-up can be opened. The following code shows how to allow opening a pop-up:

browser.set(CanOpenPopupCallback.class, (params, callback) -> callback.can());

If the CanOpenPopupCallback callback allows to open a pop-up, the OpenPopupCallback callback will be invoked. In this callback you can access the created pop-up and display it if necessary. For example:

browser.set(OpenPopupCallback.class, (params, callback) -> {
    Browser.with(params.getPopupBrowserId()).ifPresent(popup -> {
        ...
    });
    callback.proceed();
});

The popup instance in the sample above does not have any web page loaded at the time the callback is invoked. The web page will be loaded later. At this moment you can register all required event listeners and callbacks, but you cannot access DOM or JavaScript, because the popup does not have a Frame yet.

Suppressing Pop-ups

To suppress a pop-up use the following approach:

browser.set(CanOpenPopupCallback.class, (params, callback) -> callback.cannot());

Closing Pop-ups

The opened pop-up can be closed via the Browser.close() method, or via window.close() from JavaScript. In both cases the BrowserClosed event is fired.

We recommend you to always register the BrowserClosed event listener to get notifications when pop-up is closed. This will allow you to hide the pop-up window if you have it displayed.

popup.on(BrowserClosed.class, event -> {
    // Hide the pop-up window.
});
Go Top