0

Here is the code:

var newTab;
chrome.tabs.create({url: (newUrl)}, function(tab){
  newTab = tab.id;
});
check();
alert(newTab);
chrome.tabs.update(newTab, {pinned: true});
chrome.tabs.update(tabid, {active: true});
chrome.tabs.remove(newTab);

The final instruction doesn't work. the newTab variable doesn't update so that is can be pinned and then remove.

1

1 Answer 1

2

Try:

var newTab;
chrome.tabs.create({url: (newUrl)}, function(tab){
  newTab = tab.id;

  check();
  alert(newTab);
  chrome.tabs.update(newTab, {pinned: true});
  chrome.tabs.update(tabid, {active: true});
  chrome.tabs.remove(newTab);
});

// Code here is run before the tab is created.

The reason for this is that the create method on the tabs object is asynchronous. This means that any code after the method invocation will be run before the new tab has been created, causing the error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.