WebContentsView
A View that displays a WebContents.
Process: Main
This module cannot be used until the ready event of the app
module is emitted.
const { BaseWindow, WebContentsView } = require('electron')
const win = new BaseWindow({ width: 800, height: 400 })
const view1 = new WebContentsView()
win.contentView.addChildView(view1)
view1.webContents.loadURL('https://electronjs.org')
view1.setBounds({ x: 0, y: 0, width: 400, height: 400 })
const view2 = new WebContentsView()
win.contentView.addChildView(view2)
view2.webContents.loadURL('https://github.com/electron/electron')
view2.setBounds({ x: 400, y: 0, width: 400, height: 400 })
Class: WebContentsView extends View
A View that displays a WebContents.
Process: Main
WebContentsView inherits from View.
WebContentsView is an EventEmitter.
Electron's built-in classes cannot be subclassed in user code. For more information, see the FAQ.
new WebContentsView([options])
Creates a WebContentsView.
Instance Events
Event: 'bounds-changed'
Inherited from View
Emitted when the view's bounds have changed in response to being laid out. The
new bounds can be retrieved with view.getBounds().
Instance Properties
Objects created with new WebContentsView have the following properties, in
addition to those inherited from View:
view.webContents Readonly
A WebContents property containing a reference to the displayed WebContents.
Use this to interact with the WebContents, for instance to load a URL.
const { WebContentsView } = require('electron')
const view = new WebContentsView()
view.webContents.loadURL('https://electronjs.org/')
view.children Readonly
Inherited from View
A View[] property representing the child views of this view.
Instance Methods
view.addChildView(view[, index])
Inherited from View
viewView - Child view to add.indexInteger (optional) - Index at which to insert the child view. Defaults to adding the child at the end of the child list.
If the same View is added to a parent which already contains it, it will be reordered such that it becomes the topmost view.
view.removeChildView(view)
Inherited from View
viewView - Child view to remove.
If the view passed as a parameter is not a child of this view, this method is a no-op.
view.setBounds(bounds[, options])
Inherited from View
boundsRectangle - New bounds of the View.
view.getBounds()
Inherited from View
Returns Rectangle - The bounds of this View, relative to its parent.
view.setBackgroundColor(color)
Inherited from View
colorstring - Color in Hex, RGB, ARGB, HSL, HSLA or named CSS color format. The alpha channel is optional for the hex type.
Examples of valid color values:
- Hex
#fff(RGB)#ffff(ARGB)#ffffff(RRGGBB)#ffffffff(AARRGGBB)
- RGB
rgb\(([\d]+),\s*([\d]+),\s*([\d]+)\)- e.g.
rgb(255, 255, 255)
- e.g.
- RGBA
rgba\(([\d]+),\s*([\d]+),\s*([\d]+),\s*([\d.]+)\)- e.g.
rgba(255, 255, 255, 1.0)
- e.g.
- HSL
hsl\((-?[\d.]+),\s*([\d.]+)%,\s*([\d.]+)%\)- e.g.
hsl(200, 20%, 50%)
- e.g.
- HSLA
hsla\((-?[\d.]+),\s*([\d.]+)%,\s*([\d.]+)%,\s*([\d.]+)\)- e.g.
hsla(200, 20%, 50%, 0.5)
- e.g.
- Color name
- Options are listed in SkParseColor.cpp
- Similar to CSS Color Module Level 3 keywords, but case-sensitive.
- e.g.
bluevioletorred
- e.g.
Hex format with alpha takes AARRGGBB or ARGB, not RRGGBBAA or RGB.
view.setBorderRadius(radius)
Inherited from View
radiusInteger - Border radius size in pixels.
The area cutout of the view's border still captures clicks.
view.setBackgroundBlur(blurRadius)
Inherited from View
blurRadiusInteger - The radius of the background blur effect (in pixels).
You must set a background color with an alpha channel (e.g. #80ffffff) in order for the blur effect to be visible.
view.setVisible(visible)
Inherited from View
visibleboolean - If false, the view will be hidden from display.
view.getVisible()
Inherited from View
Returns boolean - Whether the view should be drawn. Note that this is
different from whether the view is visible on screen—it may still be obscured
or out of view.