windows.remove()

Closes a window and all the tabs inside it, given the window's ID.

This is an asynchronous function that returns a Promise.

Syntax

js
let removing = browser.windows.remove(
  windowId        // integer
)

Parameters

windowId

integer. ID of the window to close.

Return value

A Promise that will be fulfilled with no arguments when the window has been closed. If any error occurs, the promise will be rejected with an error message.

Examples

When the user clicks a browser action's icon, close the window in which the icon was clicked:

js
function onRemoved() {
  console.log(`Removed window`);
}

function onError(error) {
  console.error(`Error:`, error);
}

browser.browserAction.onClicked.addListener((tab) => {
  let removing = browser.windows.remove(tab.windowId);
  removing.then(onRemoved, onError);
});

Close the current, e.g., popup, window when the user clicks a button on the page:

js
// in a script loaded by the page in the window
document.querySelector("#close").addEventListener(async ({ button }) => {
  try {
    if (button) return; // not a left click
    const windowId = (await browser.windows.getCurrent()).id;
    await browser.windows.remove(windowId);
    // this point will never be reached, since the window is gone
  } catch (error) {
    console.error("Closing failed:", error);
  }
});

In Firefox, the same could be achieved with the .allowScriptsToClose window creation property and window.close().

Example extensions

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Firefox for Android
Safari on iOS
remove
Chrome – Full support
Chrome 4 (Release date: 2010-01-25)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 45 (Release date: 2016-03-08)
footnote Full support
Opera – Full support
Opera 15 (Release date: 2013-07-02)
footnote Full support
Safari – Full support
Safari 14 (Release date: 2020-09-16)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Safari on iOS – No support
Safari on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support

Note: This API is based on Chromium's chrome.windows API. This documentation is derived from windows.json in the Chromium code.