Manoj
Manoj

Reputation: 5071

Close current browser tab and focus on a browser tab of a specific url - javascript/jquery

I am building an application. In which I want close current browser tab and I want to focus on a tab that's url I'll pass. If browser contains a tab with that url then that tab should be focused and if browser is not contains a tab with that url then a new window should be opened. For closing current tab it's working window.close() but what should I do for focus on a tab.

Upvotes: 1

Views: 2060

Answers (1)

Ho Thanh Binh
Ho Thanh Binh

Reputation: 11

Try this:

// Getting a list of tabs of the current window.
chrome.windows.getLastFocused(
 // Without this, window.tabs is not populated.
 {populate: true},
 function (window)
 {
  var foundSelected = false;
  for (var i = 0; i < window.tabs.length; i++)
  {
   // Finding the selected tab.
   if (window.tabs[i].active)
   {
    foundSelected = true;
   }
   // Finding the next tab.
   else if (foundSelected)
   {
    // Selecting the next tab.
    chrome.tabs.update(window.tabs[i].id, {active: true});
    return;
   }
  }
 });

Upvotes: 1

Related Questions