docs/References/Window.md
[TOC]
Window is a wrapper of the DOM's topmost window object. It has extended operations and can receive various window events.
Every Window is an instance of the EventEmitter class, and you're able to use Window.on(...) to respond to native window's events.
!!! warning "Behavior Changed"
There are some changes of Window since 0.13.0. Please see [Migration Notes from 0.12 to 0.13](../For Users/Migration/From 0.12 to 0.13.md).
// Get the current window
var win = nw.Window.get();
// Listen to the minimize event
win.on('minimize', function() {
console.log('Window is minimized');
});
// Minimize the window
win.minimize();
// Unlisten the minimize event
win.removeAllListeners('minimize');
// Create a new window and get it
nw.Window.open('https://github.com', {}, function(new_win) {
// And listen to new window's focus event
new_win.on('focus', function() {
console.log('New window is focused');
});
});
window_object {DOM Window} Optional is the DOM window{Window} the native Window objectIf window_object is not specifed, then return current window's Window object, otherwise return window_object's Window object.
!!! note
If window_object is iframe's, the function will still return topmost window's Window object.
// Get the current window
var win = nw.Window.get();
// Get iframe's window
var iframeWin = nw.Window.get(iframe.contentWindow);
//This will return true
console.log(iframeWin === win);
// Create a new window and get it
nw.Window.open('https://github.com/nwjs/nw.js', {}, function(new_win) {
// do something with the newly created window
});
Get all windows with a callback function whose parameter is an array of nw.Window object. This function is supported since 0.42.6.
!!! warning "Behavior Changed" The behavior of the function is changed since 0.13.0. Please see [Migration Notes from 0.12 to 0.13](../For Users/Migration/From 0.12 to 0.13.md).
url {String} URL to be loaded in the opened windowoptions {Object} Optional see [Window subfields](Manifest Format.md#window-subfields) in manifest format. And following extra fields can also be used in options.
new_instance {Boolean} Optional whether to open a new window in a separate render process.mixed_context {Boolean} Optional If true, the Node context and DOM context are merged in the new window's process. Use only when new_instance is true.inject_js_start {String} Optional the script to be injected before any DOM is constructed and any script is run. See [Manifest format](Manifest Format.md#inject_js_start)inject_js_end {String} Optional the script to be injected after the document object is loaded, before onload event is fired. See [Manifest format](Manifest Format.md#inject_js_end)id {String} Optional the id used to identify the window. This will be used to remember the size and position of the window and restore that geometry when a window with the same id is later opened. See also the Chrome App documentationcallback(win) {Function} Optional callback when with the opened native Window objectOpen a new window and load url in it.
!!! note
You should wait for the Window's loaded event before interacting with any of its components.
!!! note "Focus"
The opened window is not focused by default. If you want it to be focused by default, you can set focus to true in options.
Get the corresponding DOM window object of the native window.
Get or set left/top offset from window frame to screen.
Get or set window's size, including the window's frame.
Get or set window's title.
Get or set window's menubar. Set with a Menu with type menubar. When win.menu is set to null, the menubar is completely removed for Windows and Linux, and the menubar is cleared out on Mac.
See [Customize Menubar](../For Users/Advanced/Customize Menubar.md) for the usage of menubars. And see Menu and MenuItem for detailed APIs.
Get whether the window is always on top of other windows.
Get whether we're in fullscreen mode.
Get whether transparency is turned on
Get whether we're in kiosk mode.
Get or set the page zoom. 0 for normal size; positive value for zooming in; negative value for zooming out.
This includes multiple functions to manipulate the cookies. The API is defined in the same way as Chrome Extensions. NW.js supports the get, getAll, remove and set methods; onChanged event (supporting both addListener and removeListener function on this event).
And anything related to CookieStore in the Chrome extension API is not supported, because there is only one global cookie store in NW.js apps.
x {Integer} offset to the left of the screeny {Integer} offset to the top of the screenMoves a window's left and top edge to the specified coordinates.
x {Integer} horizontal offsety {Integer} vertical offsetMoves a window a specified number of pixels relative to its current coordinates.
width {Integer} the inner width of the windowheight {Integer} the inner height of the windowResizes a window to the specified width and height.
width {Integer} the inner width of the windowheight {Integer} the inner height of the windowwidth {Integer} the offset width of the windowheight {Integer} the offset height of the windowResizes a window by the specified amount.
Focus on the window.
Move focus away. Usually it will move focus to other windows of your app, since on some platforms there is no concept of blur.
is_show {Boolean} Optional specify whether the window should be shown or hidden. It's set to true by default.Show the window if it's not shown.
show(false) has the same effect with hide().
!!! note "Focus"
show will not focus on the window on some platforms, so you need to call focus if you want to.
Hide the window. User will not be able to find the window once it's hidden.
force {Boolean} specify whether to close the window forcefully and bypass the close event.Closes the current window. You can prevent the closing by listening to the close event. If force is specified and equals true, then the close event will be ignored.
Usually you would like to listen to the close event and do some shutdown work and then do a close(true) to really close the window.
win.on('close', function () {
this.hide(); // Pretend to be closed already
console.log("We're closing...");
this.close(true); // then close it forcefully
});
win.close(); // Would be detected by the above close event. To skip the above close event use win.close(true);
Reloads the current window.
Reloads the current page by starting a new renderer process from scratch.
Like reload(), but don't use caches (aka "shift-reload").
Maximize the window on GTK and Windows, and zoom the window on Mac OS X.
!!! warning "Deprecated"
This feature is deprecated since 0.13.0. It's now replaced by restore event. See [Migration Notes from 0.12 to 0.13](../For Users/Migration/From 0.12 to 0.13.md).
Unmaximize the window, i.e. the reverse of maximize().
Minimize the window to task bar on Windows, iconify the window on GTK, and miniaturize the window on Mac OS X.
Restore window to previous state after the window is minimized, i.e. the reverse of minimize(). It's not named unminimize since restore is used commonly.
Make the window fullscreen.
!!! note
This function is different with HTML5 FullScreen API, which can make part of the page fullscreen, Window.enterFullscreen() will only fullscreen the whole window.
Leave the fullscreen mode.
Toggle the fullscreen mode.
Enter the Kiosk mode. In Kiosk mode, the app will be fullscreen and try to prevent users from leaving the app, so you should remember to provide a way in app to leave Kiosk mode. This mode is mainly used for presentation on public displays.
Leave the Kiosk mode.
Toggle the kiosk mode.
!!! warning "Deprecated" This feature is deprecated since 0.13.0. See [Migration Notes from 0.12 to 0.13](../For Users/Migration/From 0.12 to 0.13.md).
transparent {Boolean} whether to set the window to be transparentTurn on/off the transparency support. See more info on [Transparent Window](../For Users/Advanced/Transparent Window.md).
shadow {Boolean} whether the window has a shadowTurn the window's native shadow on/off. Useful for frameless, transparent windows.
!!! note This API is only available on SDK build flavor.
!!! warning "Behavior Changed" The behavior of the function is changed since 0.13.0. Please see [Migration Notes from 0.12 to 0.13](../For Users/Migration/From 0.12 to 0.13.md).
iframe {String} or {HTMLIFrameElement} Optional the id or the element of the <iframe> to be jailed on. By default, the DevTools is shown for entire window.callback(dev_win) {Function} callback with the native window of the DevTools window.Open the devtools to inspect the window.
The optional iframe as String should be the value of id attribute of any <iframe> element in the window. It jails the DevTools to inspect the <iframe> only. If it is an empty string, this feature has no effect.
The optional iframe as HTMLIFrameElement should be the iframe object. And it serves the same purpose with the id argument.
This function returns a Window object via the callback. You can use any properties and methods of Window except the events.
See also in [webview reference](webview Tag.md) on how to open DevTools for webview or open DevTools in a webview.
!!! note This API is only available on SDK build flavor.
Close the devtools window.
Enumerate the printers in the system. The callback function will receive an array of JSON objects for the printer information. The device name of the JSON object can be used as parameter in nw.Window.print().
nw.Window.isOpenDevTools((status) => console.log(status))
!!! note This API is only available on SDK build flavor.
Query the status of devtools window.
See also win.showDevTools().
Print the web contents in the window with or without the need for user's interaction. options is a JSON object with the following fields:
autoprint {Boolean} whether to print without the need for user's interaction; optional, true by defaultsilent {Boolean} hide the flashing print preview dialog; optional, false by defaultprinter {String} the device name of the printer returned by nw.Window.getPrinters(); No need to set this when printing to PDFpdf_path {String} the path of the output PDF when printing to PDFheaderFooterEnabled {Boolean} whether to enable header and footerlandscape {Boolean} whether to use landscape or portraitmediaSize {JSON Object} the paper size specshouldPrintBackgrounds {Boolean} whether to print CSS backgroundsmarginsType {Integer} 0 - Default; 1 - No margins; 2 - minimum; 3 - Custom, see marginsCustom.marginsCustom {JSON Object} the custom margin setting; units are points.copies {Integer} the number of copies to print.scaleFactor {Integer} the scale factor; 100 is the default.headerString {String} string to replace the URL in the header.footerString {String} string to replace the URL in the footer.marginsCustom example: "marginsCustom":{"marginBottom":54,"marginLeft":70,"marginRight":28,"marginTop":32}
mediaSize example: 'mediaSize':{'name': 'CUSTOM', 'width_microns': 279400, 'height_microns': 215900, 'custom_display_name':'Letter', 'is_default': true}
NOTE: If no options are being passed, win.print({}) is what should be called.
width {Integer} the maximum width of the windowheight {Integer} the maximum height of the windowSet window's maximum size.
width {Integer} the minimum width of the windowheight {Integer} the minimum height of the windowSet window's minimum size.
resizable {Boolean} whether the window can be resizedSet whether window is resizable.
top {Boolean} whether the window should always be on topSets the widget to be on top of all other windows in the window system.
visible {Boolean} whether the window should be visible on all workspacesFor platforms that support multiple workspaces (currently Mac OS X and Linux), this allows NW.js windows to be visible on all workspaces simultaneously.
Returns a boolean indicating if the platform (currently Mac OS X and Linux) supports win.setVisibleOnAllWorkspace(boolean).
position {String} the position of the window. There are three valid positions: null or center or mouseMove window to specified position. Currently only center is supported on all platforms, which will put window in the middle of the screen.
show {Boolean} whether show in task barControl whether to show window in taskbar or dock. See also show_in_taskbar in [Manifest-format](Manifest Format.md#show_in_taskbar).
attension {Boolean} or {Integer} If a Boolean, it indicates to request or cancel user's attension. If an Integer, it indicates the number of times the window flashes.Request the user's attension by making the window flashes in the task bar.
!!! note "Mac"
On Mac, value < 0 will trigger NSInformationalRequest, while value > 0 will trigger NSCriticalRequest.
callback {Function} the callback when finished capturing the windowconfig {String} or {Object} Optional if a String, see config.format for valid values.
format {String} optional the image format used to generate the image. It supports two formats: "png" and "jpeg". If ignored, it's "jpeg" by default.datatype {String} it supports three types: "raw", "buffer" and "datauri". If ignored, it's "datauri" by default.Captures the visible area of the window. To capture the full page, see win.captureScreenshot.
!!! note "raw or datauri"
The "raw" only contains the Base64 encoded image. But "datauri" contains the mime type headers as well, and it can be directly assigned to src of Image to load the image.
Example usage:
// png as base64string
win.capturePage(function(base64string){
// do something with the base64string
}, { format : 'png', datatype : 'raw'} );
// png as node buffer
win.capturePage(function(buffer){
// do something with the buffer
}, { format : 'png', datatype : 'buffer'} );
When callback is omitted, a Promise is returned and it will resolve with data argument of the callback.
callback {Function} optional the callback when finished capturing the window. It's called with:
err null for success; {String} with the error message for failure.data {String} base64 encoded imageoptions {Object}
fullSize {Boolean} optional Capture the whole page beyond the visible area. Currently the height of captured image is capped at 16384 pixels by Chromium.format {String} optional the image format used to generate the image. It supports two formats: "png" and "jpeg". "png" is the default.quality {Integer} optional Compression quality from range [0..100] (jpeg only).clip {Object} optional Capture the screenshot of a given region only. The object's properties are:
x: {Number} X offset in device independent pixels (dip).y: {Number} Y offset in device independent pixels (dip).width: {Number} Rectangle width in device independent pixels (dip).height: {Number} Rectangle height in device independent pixels (dip).scale: {Number} Page scale factor.Captures the the window. It can be used to capture the full page beyond the visible area.
!!! note Experimental This API is experimental and subject to change in the future.
Example usage:
win.captureScreenshot({fullSize: true}, (err, data) => {
if (err) {
alert(err.message);
return;
}
var image = new Image();
image.src = 'data:image/jpg;base64,' + data;
document.body.appendChild(image);
});
progress {Float} valid values within [0, 1]. Setting to negative value (<0) removes the progress bar.!!! note "Linux"
Only Ubuntu is supported, and you'll need to specify the application .desktop file through NW_DESKTOP env. If NW_DESKTOP env variable is not found, it uses nw.desktop by default.
Set the badge label on the window icon in taskbar or dock.
!!! note "Linux"
This API is only supported on Ubuntu and the label is restricted to a string number only. You'll also need to specify the .desktop file for your application (see the note on setProgressBar)
frame {HTMLIFrameElement} the frame to execute in. If iframe is null, it assumes in current window / frame.script {String} the source code of the script to be executedExecute a piece of JavaScript in the frame.
frame {HTMLIFrameElement} the frame to execute in. If iframe is null, it assumes in current window / frame.path {String|ArrayBuffer|Buffer} the path or Buffer or ArrayBuffer of the binary file generated by nwjcLoad and execute the compiled binary in the frame. See [Protect JavaScript Source Code](../For Users/Advanced/Protect JavaScript Source Code.md).
!!! warning "experimental" This API is subject to change in future versions as we're exploring ways to support this feature better. Discuss here.
frame {HTMLIFrameElement} the frame to execute in. If iframe is null, it assumes in current window / frame.path {String|ArrayBuffer|Buffer} the path or Buffer or ArrayBuffer of the binary file generated by nwjcmodule_path {String} the module URL related to the current document. It will be used to resolve the module specifier.Load and execute the compiled binary for Modules in the frame. The binary should be compiled with nwjc --nw-module. The following code will load lib.bin as module and other modules can refer to it with something like import * from './lib.js':
nw.Window.get().evalNWBinModule(null, 'lib.bin', 'lib.js');
Removes all listeners, or those of the specified eventName.
The close event is a special event that will affect the result of the Window.close() function. If developer is listening to the close event of a window, the Window.close() emit the close event without closing the window.
Usually you would do some shutdown work in the callback of close event, and then call this.close(true) to really close the window, which will not be caught again. Forgetting to add true when calling this.close() in the callback will result in infinite loop.
And if the shutdown work takes some time, users may feel that the app is exiting slowly, which is bad experience, so you could just hide the window in the close event before really closing it to make a smooth user experience.
See example code of win.close(true) above for the usage of close event.
!!! note "Mac"
On Mac, there is an argument passed to the callback indicating whether it's being closed by <kbd>⌘</kbd>+<kbd>Q</kbd>. It will be set to string quit if that's true, otherwise undefined.
The closed event is emitted after the corresponding window is closed. Normally you will not be able to get this event since after the window is closed all js objects will be released. But it is useful when listening to the window's events in another window, whose objects will not be released.
// Open a new window.
nw.Window.open('popup.html', {}, function (win) {
// Release the 'win' object here after the new window is closed.
win.on('closed', function () {
win = null;
});
// Listen to main window's close event
nw.Window.get().on('close', function () {
// Hide the window to give user the feeling of closing immediately
this.hide();
// If the new window is still open then close it.
if (win !== null) {
win.close(true);
}
// After closing the new window, close the main window.
this.close(true);
});
});
Emitted when the window starts to reload, normally you cannot catch this event because usually it's emitted before you actually setup the callback.
The only situation that you can catch this event is when you refresh the window and listen to this event in another window.
Emitted when the window is fully loaded, this event behaves the same with window.onload, but doesn't rely on the DOM.
frame {HTMLIFrameElement} is the iframe object, or null if the event is for the window.Emitted when the document object in this window or a child iframe is available, after all files are loaded, but before DOM is constructed or any script is run. It will not be fired on the new window being created with nw.Window.open(): the callback of that function will be fired at the same point of this event.
See inject_js_start in [Manifest-format](Manifest Format.md#inject_js_start).
frame {HTMLIFrameElement} is the iframe object, or null if the event is for the window.Emitted when the document object in this window or a child iframe is unloaded, but before the onunload event is emitted.
See inject_js_end in [Manifest-format](Manifest Format.md#inject_js_end)
Emitted when window gets focus.
Emitted when window loses focus.
Emitted when window is minimized.
!!! warning "Behavior Changed" The behavior of the function is changed since 0.13.0. Please see [Migration Notes from 0.12 to 0.13](../For Users/Migration/From 0.12 to 0.13.md).
Emitted when window is restored from minimize, maximize and fullscreen state.
Emitted when window is maximized.
Emitted after window is moved. The callback is called with 2 arguments: (x, y) for the new location of the left / top corner of the window.
Emitted after window is resized. The callback is called with 2 arguments: (width, height) for the new size of the window.
Emitted when window enters fullscreen state.
!!! warning "Deprecated"
This feature is deprecated since 0.13.0. It's now replaced by restore event. See [Migration Notes from 0.12 to 0.13](../For Users/Migration/From 0.12 to 0.13.md).
Emitted when window leaves fullscreen state.
Emitted when window zooming changed. It has a parameter indicating the new zoom level. See win.zoom() method for the parameter's value definition.
!!! warning "Deprecated"
This feature is deprecated since 0.13.0. Use the callback with win.capturePage() instead. See [Migration Notes from 0.12 to 0.13](../For Users/Migration/From 0.12 to 0.13.md).
Emitted after the capturePage method is called and image data is ready. See win.capturePage() callback function for the parameter's value definition.
!!! warning "Deprecated"
This feature is deprecated since 0.13.0. Use the callback passed to win.showDevtools instead. See [Migration Notes from 0.12 to 0.13](../For Users/Migration/From 0.12 to 0.13.md).
See win.showDevTools() method for more details.
Emitted after Devtools is closed.
See win.closeDevTools() method for more details.
frame {HTMLIFrameElement} is the object of the child iframe where the request is from, or null if it's from the top window.url {String} is the address of the requested linkpolicy {Object} is an object with the following methods:
ignore() : ignore the request, navigation won't happen.forceCurrent() : force the link to be opened in the same frameforceDownload() : force the link to be a downloadable, or open by external programforceNewWindow() : force the link to be opened in a new windowforceNewPopup() : force the link to be opened in a new popup windowsetNewWindowManifest(m) : control the options for the new popup window. The object m is in the same format as the [Window subfields](Manifest Format.md#window-subfields) in manifest format.Emitted when a new window is requested from this window or a child iframe. You can call policy.* methods in the callback to change the default behavior of opening new windows.
For example, you can open the URL in system brower when user tries to open in a new window:
nw.Window.get().on('new-win-policy', function(frame, url, policy) {
// do not open the window
policy.ignore();
// and open it in external browser
nw.Shell.openExternal(url);
});
frame {HTMLIFrameElement} is the object of the child iframe where the request is from, or null if it's from the top window.url {String} is the address of the requested linkpolicy {Object} is an object with the following methods:
ignore() : ignore the request, navigation won't happen.Emitted when navigating to another page. Similar to new-win-policy, you can call policy.ignore() within the callback to ignore the navigation.