Introduction
Guides
- Engine
- Browser
- BrowserView
- Navigation
- Content
- DOM
- JavaScript
- Pop-ups
- Dialogs
- Downloads
- Network
- Cache
- Cookies
- Proxy
- Authentication
- Permissions
- Plugins
- Printing
- Media
- Zoom
- Spell Checker
- Deployment
- Chromium
Troubleshoot
Migration
Pop-ups
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:
-
The
window.open()
JavaScript function. For example:window.open("https://www.google.com", "_blank", "resizable=yes, top=500, left=500, width=400, height=400");
-
A link with the
target
attribute:<a href="https://www.google.com" target="_blank">Open Google</a>
By default, all pop-ups are suppressed for the IBrowser
instance. If an IBrowserView
instance is initialized from the IBrowser
instance that does not have custom pop-up handling, the default popup handler is registered. The pop-ups are displayed in a separate Window or Form.
Opening pop-ups
To change the default behavior and take control over creation of pop-ups, use the CreatePopupHandler
and OpenPopupHandler
handlers.
The CreatePopupHandler
handler is invoked when the engine wants to know whether pop-up can be created or not. The following code sample shows how to allow creating a pop-up:
browser.CreatePopupHandler =
new Handler<CreatePopupParameters, CreatePopupResponse>(p =>
{
return CreatePopupResponse.Create();
});
If the CreatePopupHandler
handler allows you to create a pop-up, the OpenPopupHandler
handler is invoked. In this handler, you can access the created pop-up and display it if necessary. See the code sample below:
browser.OpenPopupHandler =
new Handler<OpenPopupParameters>(p =>
{
// Access the created popup browser.
IBrowser popup = p.PopupBrowser;
});
The popup
instance in the sample above does not have any web page loaded at the time the handler is invoked. The web page will be loaded later. At the moment, you can register all required event listeners and handlers, but you cannot access DOM or JavaScript, because the popup
does not have a IFrame
yet.
Suppressing pop-ups
To suppress pop-ups, use the following approach:
browser.CreatePopupHandler =
new Handler<CreatePopupParameters, CreatePopupResponse>(p =>
{
return CreatePopupResponse.Suppress();
});
Loading pop-up URL into the main browser
To suppress creating a separate pop-up and load the pop-up URL into the main browser, use the following approach:
browser.CreatePopupHandler =
new Handler<CreatePopupParameters, CreatePopupResponse>(p =>
{
browser.Navigation.LoadUrl(p.TargetUrl);
return CreatePopupResponse.Suppress();
});
Displaying pop-ups
When you initialize a WinForms or WPF BrowserView
, it automatically configures the given IBrowser
instance with the default implementation of the CreatePopupHandler
and OpenPopupHandler
handlers.
The default implementation of the CreatePopupHandler
allows creating all pop-ups:
browser.CreatePopupHandler =
new Handler<CreatePopupParameters, CreatePopupResponse>(p =>
{
return CreatePopupResponse.Create();
});
See the details on the default implementation of the OpenPopupHandler
handler for both WinForms and WPF below.
WinForms
The default implementation for WinForms BrowserView
is the following:
using System;
using System.Windows.Forms;
using DotNetBrowser.Browser;
using DotNetBrowser.Browser.Events;
using DotNetBrowser.Browser.Handlers;
using DotNetBrowser.Geometry;
using DotNetBrowser.Handlers;
namespace DotNetBrowser.WinForms
{
internal class OpenPopupHandler : IHandler<OpenPopupParameters>
{
private readonly Control parent;
#region Constructors
public OpenPopupHandler(BrowserView parent)
{
this.parent = parent;
}
#endregion
#region Methods
public void Handle(OpenPopupParameters parameters)
{
parent.BeginInvoke((Action)(() =>
{
ShowPopup(parameters.PopupBrowser,
parameters.Rectangle);
}));
}
private void ShowPopup(IBrowser popupBrowser, Rectangle rectangle)
{
BrowserView browserView = new BrowserView
{
Dock = DockStyle.Fill
};
browserView.InitializeFrom(popupBrowser);
Form form = new Form();
if (!rectangle.IsEmpty)
{
form.StartPosition = FormStartPosition.Manual;
form.Location = new System.Drawing.Point(rectangle.Origin.X,
rectangle.Origin.Y);
form.ClientSize = new System.Drawing.Size((int)rectangle.Size.Width,
(int)rectangle.Size.Height);
browserView.Width = (int)rectangle.Size.Width;
browserView.Height = (int)rectangle.Size.Height;
}
else
{
form.Width = 800;
form.Height = 600;
}
form.Closed += delegate
{
form.Controls.Clear();
if (!popupBrowser.IsDisposed)
{
popupBrowser.Dispose();
}
};
popupBrowser.TitleChanged += delegate (object sender, TitleChangedEventArgs e)
{
form.BeginInvoke((Action)(() => { form.Text = e.Title; }));
};
popupBrowser.Disposed += delegate
{
form.Controls.Clear();
form.Hide();
form.Close();
form.Dispose();
};
form.Controls.Add(browserView);
form.Show();
}
#endregion
}
}
WPF
The default implementation for WPF BrowserView
is the following:
using System;
using System.Windows;
using DotNetBrowser.Browser;
using DotNetBrowser.Browser.Events;
using DotNetBrowser.Browser.Handlers;
using DotNetBrowser.Geometry;
using DotNetBrowser.Handlers;
namespace DotNetBrowser.WPF
{
internal class OpenPopupHandler : IHandler<OpenPopupParameters>
{
private readonly FrameworkElement parent;
#region Constructors
public OpenPopupHandler(FrameworkElement parent)
{
this.parent = parent;
}
#endregion
#region Methods
public void Handle(OpenPopupParameters parameters)
{
Application.Current.Dispatcher?.BeginInvoke((Action)(() =>
{
ShowPopup(parameters.PopupBrowser,
parameters.Rectangle);
}));
}
private void ShowPopup(IBrowser popupBrowser, Rectangle rectangle)
{
BrowserView browserView = new BrowserView();
browserView.InitializeFrom(popupBrowser);
Window window = new Window();
window.Owner = Window.GetWindow(parent);
if (!rectangle.IsEmpty)
{
window.Top = rectangle.Origin.Y;
window.Left = rectangle.Origin.X;
window.SizeToContent = SizeToContent.WidthAndHeight;
browserView.Width = rectangle.Size.Width;
browserView.Height = rectangle.Size.Height;
}
else
{
window.Width = 800;
window.Height = 600;
}
window.Closed += delegate
{
window.Content = null;
if (!popupBrowser.IsDisposed)
{
popupBrowser.Dispose();
}
};
popupBrowser.TitleChanged += delegate (object sender, TitleChangedEventArgs e)
{
Application.Current
.Dispatcher?.BeginInvoke((Action)(() => window.Title = e.Title));
};
popupBrowser.Disposed += delegate
{
Application.Current.Dispatcher?.Invoke(() =>
{
window.Content = null;
window.Hide();
window.Close();
});
};
window.Content = browserView;
window.Show();
}
#endregion
}
}
Closing pop-ups
The opened pop-up can be closed using the IBrowser.Dispose()
method or window.close()
from JavaScript. In both cases, the Disposed
event is fired.
We recommend that you always register the Disposed
event listener to get notifications when a pop-up is closed. This will allow you to hide the pop-up window if you have it displayed.
popupBrowser.Disposed += delegate
{
// Hide the pop-up window.
};