Madhur Ahuja
Madhur Ahuja

Reputation: 22709

javascript callback not working

I have a code like this

function SocialMiner()     
{
    var verbose=true;
    var profileArray=new Array();
    var tabUrl;
    this.getTabUrl=function(callback)
    {
        chrome.tabs.getSelected(null, function(tab)
        {
            myUrl = tab.url;
            console.log("0"+tab.url);
            console.log("calling callback");
            callback.call(tab.url);

        });
    }    

    this.setTabUrlValue=function(pageUrl)
    {
        console.log("1"+pageUrl);
        tabUrl=pageUrl;
    }
};

I call the first method with second as callback

 var pageUrl=miner.getTabUrl(miner.setTabUrlValue);

What I observe is that , second function does not receives the value, i.e. pageUrl is undefined, however it was correctly passed in first function. Any pointers ?

Upvotes: 1

Views: 863

Answers (1)

Andrew Whitaker
Andrew Whitaker

Reputation: 126082

Your syntax to call is incorrect; the first parameter to call is what determines the value of this inside of the function you're calling. The second argument is where you would place an array of arguments to pass to the function

You could simply use

callback(tab.url);

in this case.

If you wanted to use call:

callback.call(this, tab.url);

Upvotes: 4

Related Questions