Skip to content

docs: update the example of webContents.setWindowOpenHandler to cla…#49379

Merged
jkleinsc merged 1 commit intoelectron:mainfrom
z0gSh1u:main
Mar 16, 2026
Merged

docs: update the example of webContents.setWindowOpenHandler to cla…#49379
jkleinsc merged 1 commit intoelectron:mainfrom
z0gSh1u:main

Conversation

@z0gSh1u
Copy link
Copy Markdown
Contributor

@z0gSh1u z0gSh1u commented Jan 13, 2026

…rify background-tab disposition cases

Description of Change

Resolves #49307 to some extent.

Current createWindow example in webContents.setWindowOpenHandler doesn't work well for background-tab disposition (Middle-click, or CmdOrCtrl+Left click). It creates a transparent view that nothing paints on it when using BrowserView, and causes a main process error for WebContentsView.

The root cause is that Chromium would defer the webContents of background-tab to become ready, so that options.webContents is undefined. See https://github.com/chromium/chromium/blob/e7662a747d6215ec7c31cd1b226dd38d97fa8baa/content/public/browser/web_contents_delegate.h#L161-L164 and https://github.com/chromium/chromium/blob/main/content/browser/web_contents/web_contents_impl.cc#L5306-L5307, saying that OpenURLFromTab "returns nullptr if the URL wasn't opened immediately", and the new background tab is not shown immediately.

As it's the intended behavior of Chromium, this PR clarifies this point by updating the example, so that the developers can treat this case properly.

A possible concern is that loading the URL by ourselves instead of inheriting the WebContents provided by options might result in a null window.opener. But after testing on Chrome, Firefox and Safari, all of them don't keep the track of window.opener for background-tab disposition for something like <a href="..." target="_blank" rel="opener">...</a>. So it's okay.

Checklist

Release Notes

Notes: Updated the example of webContents.setWindowOpenHandler to clarify background-tab disposition cases

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the documentation example for webContents.setWindowOpenHandler to properly handle the background-tab disposition case (triggered by middle-click or Ctrl/Cmd+click). The update addresses an issue where Chromium defers the webContents for background tabs, causing it to be undefined in the createWindow callback.

Changes:

  • Added a conditional check to manually load the URL for background-tab disposition cases
  • Added an explanatory comment describing why manual URL loading is necessary

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread docs/api/web-contents.md Outdated
const browserView = new BrowserView(options)
mainWindow.addBrowserView(browserView)
browserView.setBounds({ x: 0, y: 0, width: 640, height: 480 })
// `background-tab` disposition defers `options.webContents` to become ready here,
Copy link

Copilot AI Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment wording could be clearer. Instead of saying "defers options.webContents to become ready", it would be more accurate to say that options.webContents is undefined for background-tab disposition. Consider rephrasing to: "For background-tab disposition, options.webContents is undefined, so load the URL manually."

Suggested change
// `background-tab` disposition defers `options.webContents` to become ready here,
// For `background-tab` disposition, options.webContents is undefined,

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

@nikwen nikwen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this!

Comment thread docs/api/web-contents.md Outdated
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Jan 14, 2026
@erickzhao erickzhao added target/40-x-y PR should also be added to the "40-x-y" branch. target/41-x-y PR should also be added to the "41-x-y" branch. labels Jan 20, 2026
@erickzhao erickzhao removed the target/40-x-y PR should also be added to the "40-x-y" branch. label Mar 3, 2026
@mlaurencin
Copy link
Copy Markdown
Member

@z0gSh1u Would you be able to rebase your PR on the latest main? That should help pass ci

Copy link
Copy Markdown
Member

@erickzhao erickzhao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs: lgtm

Copy link
Copy Markdown

@electron-docs-reviewer electron-docs-reviewer Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving on behalf of the Electron Docs Team

@z0gSh1u
Copy link
Copy Markdown
Contributor Author

z0gSh1u commented Mar 4, 2026

@z0gSh1u Would you be able to rebase your PR on the latest main? That should help pass ci

OK. Latest main merged.

@jkleinsc
Copy link
Copy Markdown
Member

@z0gSh1u we require signed commits before merging: https://www.electronjs.org/docs/latest/development/pull-requests#commit-signing. Can you please sign your commit?

@mlaurencin mlaurencin added the needs-signed-commits Currently some or all of the commits in this PR are not signed label Mar 13, 2026
@github-actions github-actions Bot added the target/42-x-y PR should also be added to the "42-x-y" branch. label Mar 13, 2026
@z0gSh1u
Copy link
Copy Markdown
Contributor Author

z0gSh1u commented Mar 14, 2026

@z0gSh1u we require signed commits before merging: https://www.electronjs.org/docs/latest/development/pull-requests#commit-signing. Can you please sign your commit?

OK. I've squashed commits into one, and signed the commit.

@electron-issue-triage electron-issue-triage Bot removed the needs-signed-commits Currently some or all of the commits in this PR are not signed label Mar 14, 2026
@jkleinsc jkleinsc merged commit 078586f into electron:main Mar 16, 2026
32 checks passed
@welcome
Copy link
Copy Markdown

welcome Bot commented Mar 16, 2026

Congrats on merging your first pull request! 🎉🎉🎉

@release-clerk
Copy link
Copy Markdown

release-clerk Bot commented Mar 16, 2026

Release Notes Persisted

Updated the example of webContents.setWindowOpenHandler to clarify background-tab disposition cases

@trop
Copy link
Copy Markdown
Contributor

trop Bot commented Mar 16, 2026

I have automatically backported this PR to "42-x-y", please check out #50292

@trop
Copy link
Copy Markdown
Contributor

trop Bot commented Mar 16, 2026

I have automatically backported this PR to "41-x-y", please check out #50293

@trop trop Bot added in-flight/42-x-y in-flight/41-x-y merged/41-x-y PR was merged to the "41-x-y" branch. merged/42-x-y PR was merged to the "42-x-y" branch. and removed target/42-x-y PR should also be added to the "42-x-y" branch. target/41-x-y PR should also be added to the "41-x-y" branch. in-flight/41-x-y in-flight/42-x-y labels Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation 📓 merged/41-x-y PR was merged to the "41-x-y" branch. merged/42-x-y PR was merged to the "42-x-y" branch. semver/patch backwards-compatible bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

windowOpenHandler's createWindow receives undefined options.webContents for background-tab disposition

7 participants