Reputation: 105
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?
Upvotes: 8
Views: 18619
Reputation: 348992
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"});
Upvotes: 8
Reputation: 1628
You have to tell it which tab (tab.id) you want to update.
chrome.tabs.update(id,{url:"http://en.wikipedia.org"});
Upvotes: 0