8
function doit() {
 alert(3);
 // Statement 1
 // chrome.tabs.create({url:"http://www.google.com"});
 // Statement 2
 // chrome.tabs.update({url:"http://en.wikipedia.org"});
 alert(4);
}


chrome.browserAction.onClicked.addListener(doit);

When the script runs as is, I get JS alerts of 3 and 4. OK.

When I comment in statement 1 and run the script, I get a JS alert of 3, then Google opens in a new, active tab, then I get a JS alert of 4. As expected.

When I comment out statement 1 and comment in statement 2, I get a JS alert of 3, and that's it.

According to http://code.google.com/chrome/extensions/tabs.html#method-update, I do not need to pass a tabId object, because it "defaults to the selected tab of the current window." The url object is defined as "A URL to navigate the tab to", as I noticed when I ran the chrome.tabs.create in statement 1.

Why does my chrome.tabs.update statement not work?

mdslup
  • 105
  • 1
  • 1
  • 8

2 Answers2

8

The tabId parameter has to be specified. Supply null if you want to use the default settings. Otherwise, the following error occurs:

Error in event handler for 'browserAction.onClicked': Error: Invalid value for argument 1. Expected 'integer' but got 'object'.

This message is logged at the background page. To access this console, follow the four steps in this answer.

Corrected code: chrome.tabs.update(null, {url:"http://en.wikipedia.org"});

Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • This works. Thanks. My definition of 'optional argument' has always been just that...it does not have to be passed. Also, thanks for the info on how to access the background page. – mdslup Apr 29 '12 at 08:47
0

You have to tell it which tab (tab.id) you want to update.

chrome.tabs.update(id,{url:"http://en.wikipedia.org"});
Robert Louis Murphy
  • 1,558
  • 1
  • 16
  • 29
  • 1
    According to , the tabId is optional and "default to the selected tab of the current window". – mdslup Apr 27 '12 at 12:45