Наши волонтёры ещё не перевели данную статью на Русский. Присоединяйтесь к нам и помогите сделать эту работу!
Вы можете также прочитать эту статью на English (US).

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

This is an asynchronous function that returns a Promise.

Syntax

var 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.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxOperaFirefox for Android
removeChrome Full support YesEdge No support NoFirefox Full support 45Opera Full support YesFirefox Android No support No

Legend

Full support  
Full support
No support  
No support

Examples

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

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

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

browser.browserAction.onClicked.addListener((tab) => {

  var 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:

// 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 acheived with the .allowScriptsToClose window creation property and window.close().

Example extensions

Acknowledgements

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

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

Метки документа и участники

Внесли вклад в эту страницу: mdnwebdocs-bot, fscholz, NiklasGollenstede, wbamberg, Makyen, chrisdavidmills
Обновлялась последний раз: mdnwebdocs-bot,