Results 1 to 5 of 5

Thread: jQuery function return value

  1. #1

    Thread Starter
    Addicted Member *PsyKE1*'s Avatar
    Join Date
    Jun 2010
    Location
    Spain
    Posts
    243

    jQuery function return value

    I am very new to this, I made this function:
    Code:
    function getID(user){
        $.getJSON('https://twitter.com/users/'+user+'.json?callback=?',
        function(data)
        {
            $('#userid').html(data.id_str);
        });
    }
    Works well, but I want that instead of modifying the value of #userid I return a value, I tried this:
    Code:
    function getID(user){
        $.getJSON('https://twitter.com/users/'+user+'.json?callback=?',
        function(data)
        {
            return data.id_str;
        });
    }
    But it makes me return to the function within which it is, not getID.
    Solutions?

    Thanks!

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: jQuery function return value

    getJSON() is an asynchronous method, which I don't think can be used in that way. But getJSON() is essentially just a wrapper for jQuery's more generic ajax() method, which allows you to set its async property (it notes that JSONP cannot support synchronous operation, but it doesn't look like you need that). From there, you could get the jqXHR object that is returned by ajax() and parse that for your result.

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: jQuery function return value

    Now - I'm curious - why are you calling a twitter service like that?

    Would they want to know you are doing that??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    Addicted Member *PsyKE1*'s Avatar
    Join Date
    Jun 2010
    Location
    Spain
    Posts
    243

    Re: jQuery function return value

    getJSON() is an asynchronous method, which I don't think can be used in that way. But getJSON() is essentially just a wrapper for jQuery's more generic ajax() method, which allows you to set its async property (it notes that JSONP cannot support synchronous operation, but it doesn't look like you need that). From there, you could get the jqXHR object that is returned by ajax() and parse that for your result.
    Ok, thanks, I'll investigate.

    Now - I'm curious - why are you calling a twitter service like that?

    Would they want to know you are doing that??
    I just get the id of a twitter user.

    Thanks! :P

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: jQuery function return value

    You might be interested in the Twitter API.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width