Search code examples
javascriptgoogle-chromegoogle-chrome-extension

JavaScript/Chrome Extension Opening a New Tab In The Second Slot


I would like to make a function to open a new tab in the second slot of google chrome for instance. I have been using the manual so I am unsure what the exact mistake is. For instance, if there are five tabs open, I want the new tab to open between the original first and second ones.

  var newURL = "http://google.com/";
  chrome.tabs.create({url: newURL, tabId: 2});

Solution

  • You will need to pass the index in your options argument in order to create a new tab at a specific position.

    Example:

    var newURL = "http://google.com/";
    // Starts at 0. Therefore the new tab will be created in the second position
    chrome.tabs.create({url: newURL, index: 1});
    
    

    Reference: https://developer.chrome.com/docs/extensions/reference/tabs/#method-create