Prevent opening new windows #768

Open
opened 2026-01-29 16:48:09 +00:00 by claunia · 1 comment
Owner

Originally created by @delasource on GitHub (Mar 3, 2022).

There is electrons "new-window" event. But i can not call event.preventDefault() in C# code (event browserWindow.OnNewWindowForTab). How am i be able to prevent opening new windows of my application? (for example when the user middle-mouse-clicks a link)

However, it is marked deprecated in the documentation in favor of this function on the webContents class. So how can i access that? There are plenty of methods that are not implemented in Electron.Net.

Related to #434

Originally created by @delasource on GitHub (Mar 3, 2022). There is electrons ["new-window"](https://github.com/electron/electron/blob/main/docs/api/web-contents.md#event-new-window-deprecated) event. But i can not call `event.preventDefault()` in C# code (event `browserWindow.OnNewWindowForTab`). How am i be able to prevent opening new windows of my application? (for example when the user middle-mouse-clicks a link) However, it is marked deprecated in the documentation in favor of [this function on the webContents class](https://github.com/electron/electron/blob/main/docs/api/web-contents.md#contentssetwindowopenhandlerhandler). So how can i access that? There are plenty of methods that are not implemented in Electron.Net. Related to #434
claunia added the Feature label 2026-01-29 16:48:09 +00:00
Author
Owner

@danatcofo commented on GitHub (Mar 3, 2022):

you have to do that in the javascript on the page itself. You can't do everything in .net.

Here is the code I use to prevent navigating away. For other similar scenarios I would suggest doing a google search. You have somewhat more freedom in the electron world than a standard browser.

// this prevents the window from navigation away.
  document.addEventListener('DOMContentLoaded', () => {
    document.addEventListener('click', (e) => {
      if (e.target.href && e.target.host != window.location.host) {
        e.preventDefault();
        e.stopPropagation();
        setTimeout(() => {
          // do something else here
        }, 100);
        return false;
      }
    }, true);
  });
@danatcofo commented on GitHub (Mar 3, 2022): you have to do that in the javascript on the page itself. You can't do everything in .net. Here is the code I use to prevent navigating away. For other similar scenarios I would suggest doing a google search. You have somewhat more freedom in the electron world than a standard browser. ```javascript // this prevents the window from navigation away. document.addEventListener('DOMContentLoaded', () => { document.addEventListener('click', (e) => { if (e.target.href && e.target.host != window.location.host) { e.preventDefault(); e.stopPropagation(); setTimeout(() => { // do something else here }, 100); return false; } }, true); }); ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#768