How to assign a chrome tab to a variable by a given tab id in javascript? -
i tried assign tab variable; failed. here code:
var tab; chrome.tabs.get(id, function(t) { tab = t; console.log("tab: " + tab); // here printed result right }); console.log("(after assignment) tab: " + tab); // "undefined"
the code simple don't know problem is...
the problem in asynchronous chrome api calls, meaning don't receive response them immediately. can rewrite code in way example:
var tab; chrome.tabs.get(id, function(t) { tab = t; console.log("tab: " + tab); // here printed result right afterassignment(); }); function afterassignment() { console.log("(after assignment) tab: " + tab); // "undefined" //the rest of code needs use tab var }
Comments
Post a Comment