W3cubDocs

/Web Extensions

tabs.discard()

Discards one or more tabs.

Some browsers automatically "discard" tabs that they don't think are likely to be needed by the user soon. The tab stays visible in the tabstrip and the browser remembers its state, so if the user selects a tab that has been discarded, it is immediately restored.

The details of exactly what is discarded are browser-specific, but in general, discarding a tab enables the browser to free some of the memory occupied by that tab.

The tabs.discard() API enables an extension to discard one or more tabs. It's not possible to discard the currently active tab, or a tab whose document contains a beforeunload listener that would display a prompt.

This is an asynchronous function that returns a Promise.

Syntax

var discarding = browser.tabs.discard(
  tabIds          // integer or integer array
)

Parameters

tabIds
integer or array of integer. The IDs of the tab or tabs to discard.

Return value

A Promise that will be fulfilled with no arguments when all the specified tabs have been discarded. If any error occurs (for example, invalid tab IDs), the promise will be rejected with an error message.

If the ID of the active tab is passed in, it will not be discarded, but the promise will be fulfilled and any other tabs passed in will be discarded.

Examples

Discard a single tab:

function onDiscarded() {
  console.log(`Discarded`);
}

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

var discarding = browser.tabs.discard(2);
discarding.then(onDiscarded, onError);

Discard multiple tabs:

function onDiscarded() {
  console.log(`Discarded`);
}

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

var discarding = browser.tabs.discard([15, 14, 1]);
discarding.then(onDiscarded, onError);

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
discard
54
["Only accepts a single tab ID as a parameter, not an array.", "The tab ID argument is optional: if it is omitted, the browser discards the least important tab.", "The callback is passed a Tab object representing the tab that was discarded.", "Tabs whose document contains a beforeunload listener that displays a prompt will be discarded."]
14
58
?
Yes
["Only accepts a single tab ID as a parameter, not an array.", "The tab ID argument is optional: if it is omitted, the browser discards the least important tab.", "The callback is passed a Tab object representing the tab that was discarded.", "Tabs whose document contains a beforeunload listener that displays a prompt will be discarded."]
No
?
?
No
?
?
?

Note: This API is based on Chromium's chrome.tabs API.

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/discard