Contents

Popups

This page describes how to handle popup windows in your application.

Molybden allows you to create and show multiple application windows. The number of windows is limited by the amount of memory available on the user’s computer. You control all application windows and decide when to create, show, hide, or destroy them.

There’s a special type of application window called “JavaScript Popup Window” or “Popup”. This type of window is created and shown by the application automatically in the following two cases:

  1. When the user clicks a link with the target="_blank" attribute.
  2. When JavaScript calls the window.open() function.

When one of these two events occurs, Molybden creates and shows a new popup window.

Opening popups

Molybden provides API that allows you to handle the moment when the popup window is about to be opened. To do that, you can use the following approach:

browser->onOpenPopup = [](const OpenPopupArgs& args, OpenPopupAction action) {
  // Access the created popup window.
  auto popup = args.popup_browser;
  // Configure popup window title visibility.
  popup->setWindowTitleVisible(false);
  action.open();
};

In the example above we access the created popup window and configure its title visibility. Then we call the open() function to show the popup window and load the required content.

Important: the popup instance in the example above does not have any content loaded at the time the delegate is invoked. The content will be loaded later. At this moment you can register all required event listeners and delegates, configure popup window, but you cannot access DOM or execute JavaScript, because it hasn’t been loaded yet.

The window.open() JavaScript function accepts the optional windowFeatures parameter that allows you to configure the popup window. The features parameter is a string that contains a list of features separated by commas. Each feature has the following format: name=value. For example:

window.open("https://google.com", "Google", "left=100,top=100,width=320,height=320");

Molybden supports only the features that allows you to configure the position and size of the popup window. You can access the given position and size using OpenPopupArgs::initial_bounds:

browser->onOpenPopup = [](const OpenPopupArgs& args, OpenPopupAction action) {
  // Access the created popup window.
  auto popup = args.popup_browser;
  // Set the initial bounds obtained from the popup window features.
  popup->setBounds(args.initial_bounds);
  action.open();
};

Suppressing popups

If you don’t need to open popup window, you can suppress it as shown in the code snippet below:

browser->onOpenPopup = [](const OpenPopupArgs& args, OpenPopupAction action) {
  action.close();
};

Now, any web page loaded in the browser instance will not be able to open popup windows.

Closing popups

The popup window can be closed in the following ways:

  1. The user clicks the close button in the title bar of the popup window.
  2. When JavaScript calls the window.close() function.
  3. When application calls the molybden::Browser::close() function.

To get notifications when popup window is closed, you can use the following approach:

popup_browser->onBrowserClosed += [](const BrowserClosed& event) {
  // The popup window is closed.
};
On this page
Top