Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close the current extension tab?

Tags:

I'm trying to close the Options page of the extension. I have a Cancel button and I'm using this code:

chrome.tabs.getCurrent(null, function(tab) {   chrome.tabs.remove(tab.id, function() {}); }); 

When I'm trying to use it, it always gives this error:

Uncaught TypeError: Cannot call method 'getCurrent' of undefined 

What's wrong with the code?

like image 221
xx55tt Avatar asked Nov 12 '11 14:11

xx55tt


People also ask

How do I close all open tabs?

Chrome: Select Settings > On startup > Open the New Tab page. Edge: Select X > Close All and check Always close all tabs. Android Chrome/Firefox: Select Tab > three dots > Close all tabs.

How do I close the current page in Javascript?

The Window. close() method closes the current window, or the window on which it was called. This method can only be called on windows that were opened by a script using the Window. open() method.


1 Answers

It works for me with one little fix:

chrome.tabs.getCurrent(function(tab) {     chrome.tabs.remove(tab.id, function() { }); }); 

Just make sure you're really running this code in options page of your extension and not just some HTML page, because chrome.tabs API is available only for extensions.

like image 176
martin Avatar answered Oct 16 '22 01:10

martin