Return Value Hitskin_logo Hitskin.com

This is a Hitskin.com skin preview
Install the skinReturn to the skin page

The forum of the forums
Would you like to react to this message? Create an account in a few clicks or log in to continue.
3 posters

    Return Value

    TamDonCo
    TamDonCo
    Forumember


    Posts : 427
    Reputation : 2
    Language : English

    Solved Return Value

    Post by TamDonCo September 14th 2016, 8:41 pm

    Code:
    $(function(){

        var str = ABC(ID);  // str = strReturn

    });

    function ABC(ID) {

      $.get('/u' + ID + '/profile?mode=editprofile&page_profil=friendsfoes', function (e) {

          var strReturn;

          // do somthing here and strReturn has some value
          // return strReturn
          // e.g strReturn = "123"
         
          strReturn = "123";
         
          return(strReturn);  // does not work
      })
    }

    How do I get the return value ??


    Last edited by TamDonCo on September 16th 2016, 3:46 pm; edited 1 time in total
    SLGray
    SLGray
    Administrator
    Administrator


    Male Posts : 51499
    Reputation : 3523
    Language : English
    Location : United States

    Solved Re: Return Value

    Post by SLGray September 14th 2016, 10:51 pm

    It would help if you exactly explain what this script does?



    Return Value Slgray10

    When your topic has been solved, ensure you mark the topic solved.
    Never post your email in public.
    Ch@lo Valdez
    Ch@lo Valdez
    Forumember


    Male Posts : 138
    Reputation : 50
    Language : spanish

    Solved Re: Return Value

    Post by Ch@lo Valdez September 15th 2016, 4:21 am

    Code:

    function ABC(ID) {
     
      $.get('/u' + ID + '/profile?mode=editprofile&page_profil=friendsfoes', function (e) {
     
          var strReturn = $('.panel:eq(4)', e).html();
     
        typeof  strReturn !== "undefined" && (document.body.insertAdjacentHTML('afterbegin', strReturn));
      })
    }

    if you want some element of friend page, you need set it in a var search in ajax dom and you can insert this element werever you want

    with
    Code:
    ABC(1)
    or any other id
    TamDonCo
    TamDonCo
    Forumember


    Posts : 427
    Reputation : 2
    Language : English

    Solved Re: Return Value

    Post by TamDonCo September 15th 2016, 3:57 pm

    Thanks Ch@lo Valdez

    I try my own ID first but it returns "undefined"
    Ch@lo Valdez
    Ch@lo Valdez
    Forumember


    Male Posts : 138
    Reputation : 50
    Language : spanish

    Solved Re: Return Value

    Post by Ch@lo Valdez September 15th 2016, 9:42 pm

    What kind of data you try to return?
    TamDonCo
    TamDonCo
    Forumember


    Posts : 427
    Reputation : 2
    Language : English

    Solved Re: Return Value

    Post by TamDonCo September 16th 2016, 12:18 am

    TamDonCo wrote:Thanks Ch@lo Valdez

    I try my own ID first but it returns "undefined"


    just a string, I test

    Code:
    var str = ABC(ID);

    it returns "undefined"

    Ch@lo Valdez
    Ch@lo Valdez
    Forumember


    Male Posts : 138
    Reputation : 50
    Language : spanish

    Solved Re: Return Value

    Post by Ch@lo Valdez September 16th 2016, 12:21 am

    what kind of string? is important you write here your complete idea, otherwise no one can help
    TamDonCo
    TamDonCo
    Forumember


    Posts : 427
    Reputation : 2
    Language : English

    Solved Re: Return Value

    Post by TamDonCo September 16th 2016, 12:53 am

    Ch@lo Valdez wrote:what kind of string? is important you write here your complete idea, otherwise no one can help

    just get the foes list from current login user , not others

    the function ABC get the foes list , I can do that part and return that value back to main function
    TamDonCo
    TamDonCo
    Forumember


    Posts : 427
    Reputation : 2
    Language : English

    Solved Re: Return Value

    Post by TamDonCo September 16th 2016, 1:41 am

    This is my sample , just ignore line 18-65 , only string manipulation, the final string on line 67 and good but failed to return value to main function on line 70

    Code:
    $(function(){
         
        // guest login, just exit
          if (! (_userdata.session_logged_in)) return;

          var uID = _userdata.user_id ;
          var foesstring = getFoesList(uID);
          window.console && console.log("!" + foesstring);

    });

    function getFoesList(uID) {



      $.get('/u' + uID + '/profile?mode=editprofile&page_profil=friendsfoes', function (e) {
         
        var n1, n2;
        n2 = e.indexOf('>Foes list<');
        n1 = e.indexOf('>Friends list<');
        var strFoes = e.substr(n2);
        var strFriends = e.substr(n1, n2 - n1);

        var n = 0;
        var nEnd = 0;
        var Friends = "";
        var FriendsID = "";

        while (n >= 0) {
          n= strFriends.indexOf('friends-foes-list');
          if (n >= 0) {
            strFriends = strFriends.substr(n+30);
            nEnd = strFriends.indexOf('">') ;
            FriendsID = FriendsID + "," +  strFriends.substr(0,nEnd);

            strFriends = strFriends.substr(nEnd+2);
            nEnd = strFriends.indexOf('</a>') ;
            Friends = Friends + "," +  strFriends.substr(0,nEnd);
          }
        }

        FriendsID = FriendsID.substr(1);
        Friends = Friends.substr(1);

        n = 0 ;
        nEnd = 0;
        var Foes = "";
        var FoesID = "";
         
        while (n >= 0) {
          n= strFoes.indexOf('friends-foes-list');
          if (n >= 0) {
            strFoes = strFoes.substr(n+30);
            nEnd = strFoes.indexOf('">') ;
            FoesID = FoesID + "," +  strFoes.substr(0,nEnd);

            strFoes= strFoes.substr(nEnd+2);
            nEnd = strFoes.indexOf('</a>') ;
            Foes = Foes + "," +  strFoes.substr(0,nEnd);
          }
        }

        FoesID = FoesID.substr(1);
        Foes = Foes.substr(1);

        // It 's OK until here, get all foes ID
        window.console && console.log(FoesID);

        // Return failed and get undefined
        return FoesID;

      });
       
    }
    Ch@lo Valdez
    Ch@lo Valdez
    Forumember


    Male Posts : 138
    Reputation : 50
    Language : spanish

    Solved Re: Return Value

    Post by Ch@lo Valdez September 16th 2016, 7:30 am

    ok try this i think inside of get ajax you can't return string or maybe i dont know how lol

    but look this:

    Code:

    $(function(){
        // guest login, just exit
        if (! (_userdata.session_logged_in)) return;
        var uID = _userdata.user_id ;
        var foesstring = getFoesList(uID);
        window.console && console.log("!" + foesstring);
    });
    function getFoesList(uID) {
        $.get('/u' + uID + '/profile?mode=editprofile&page_profil=friendsfoes', function (e) {
            var n1, n2;
            n2 = e.indexOf('>Foes list<');
            n1 = e.indexOf('>Friends list<');
            var strFoes = e.substr(n2);
            var strFriends = e.substr(n1, n2 - n1);
            var n = 0;
            var nEnd = 0;
            var Friends = "";
            var FriendsID = "";
            while (n >= 0) {
                n= strFriends.indexOf('friends-foes-list');
                if (n >= 0) {
                    strFriends = strFriends.substr(n+30);
                    nEnd = strFriends.indexOf('">') ;
                    FriendsID = FriendsID + "," +  strFriends.substr(0,nEnd);
                    strFriends = strFriends.substr(nEnd+2);
                    nEnd = strFriends.indexOf('</a>') ;
                    Friends = Friends + "," +  strFriends.substr(0,nEnd);
                }
            }
            FriendsID = FriendsID.substr(1);
            Friends = Friends.substr(1);
            n = 0 ;
            nEnd = 0;
            var Foes = "";
            var FoesID = "";
            while (n >= 0) {
                n= strFoes.indexOf('friends-foes-list');
                if (n >= 0) {
                    strFoes = strFoes.substr(n+30);
                    nEnd = strFoes.indexOf('">') ;
                    FoesID = FoesID + "," +  strFoes.substr(0,nEnd);
                    strFoes= strFoes.substr(nEnd+2);
                    nEnd = strFoes.indexOf('</a>') ;
                    Foes = Foes + "," +  strFoes.substr(0,nEnd);
                }
            }
            FoesID = FoesID.substr(1);
            Foes = Foes.substr(1);
            // It 's OK until here, get all foes ID
            window.console && console.log(FoesID);
            // inside of $.get function maybe is better storage the data in cookie or window.storage and call in another function, just try it
            my_setcookie('friendsid'+uID, FoesID);
            return_friends(uID)
        });
    }
    function return_friends(c){
        return my_setcookie('friendsid'+c);
    }
    //use return_friends(id) example: document.body.innerHTML = return_friends(_userdata.user_id)
    Ch@lo Valdez
    Ch@lo Valdez
    Forumember


    Male Posts : 138
    Reputation : 50
    Language : spanish

    Solved Re: Return Value

    Post by Ch@lo Valdez September 16th 2016, 7:40 am

    mmm or just use a universal variable

    Code:

    $(function(){
        // guest login, just exit
        if (! (_userdata.session_logged_in)) return;
        var uID = _userdata.user_id ;
        var foesstring = getFoesList(uID);
        window.console && console.log("!" + foesstring);
    });
    function getFoesList(uID) {
        $.get('/u' + uID + '/profile?mode=editprofile&page_profil=friendsfoes', function (e) {
            var n1, n2;
            n2 = e.indexOf('>Foes list<');
            n1 = e.indexOf('>Friends list<');
            var strFoes = e.substr(n2);
            var strFriends = e.substr(n1, n2 - n1);
            var n = 0;
            var nEnd = 0;
            var Friends = "";
            var FriendsID = "";
            while (n >= 0) {
                n= strFriends.indexOf('friends-foes-list');
                if (n >= 0) {
                    strFriends = strFriends.substr(n+30);
                    nEnd = strFriends.indexOf('">') ;
                    FriendsID = FriendsID + "," +  strFriends.substr(0,nEnd);
                    strFriends = strFriends.substr(nEnd+2);
                    nEnd = strFriends.indexOf('</a>') ;
                    Friends = Friends + "," +  strFriends.substr(0,nEnd);
                }
            }
            FriendsID = FriendsID.substr(1);
            Friends = Friends.substr(1);
            n = 0 ;
            nEnd = 0;
            var Foes = "";
            var FoesID = "";
            while (n >= 0) {
                n= strFoes.indexOf('friends-foes-list');
                if (n >= 0) {
                    strFoes = strFoes.substr(n+30);
                    nEnd = strFoes.indexOf('">') ;
                    FoesID = FoesID + "," +  strFoes.substr(0,nEnd);
                    strFoes= strFoes.substr(nEnd+2);
                    nEnd = strFoes.indexOf('</a>') ;
                    Foes = Foes + "," +  strFoes.substr(0,nEnd);
                }
            }
            FoesID = FoesID.substr(1);
            Foes = Foes.substr(1);
            // It 's OK until here, get all foes ID
            window.console && console.log(FoesID);
         
          foesID = FoesID;
         
        });
    }


    just use
    Code:
    foesID
    and return string

    or this way:
    Code:


    $(function(){
        // guest login, just exit
        if (! (_userdata.session_logged_in)) return;
        var uID = _userdata.user_id ;
        var foesstring = getFoesList(uID);
        window.console && console.log("!" + foesstring);
    });
    function getFoesList(uID) {
    var foesID; // <----- new var
        $.get('/u' + uID + '/profile?mode=editprofile&page_profil=friendsfoes', function (e) {
            var n1, n2;
            n2 = e.indexOf('>Foes list<');
            n1 = e.indexOf('>Friends list<');
            var strFoes = e.substr(n2);
            var strFriends = e.substr(n1, n2 - n1);
            var n = 0;
            var nEnd = 0;
            var Friends = "";
            var FriendsID = "";
            while (n >= 0) {
                n= strFriends.indexOf('friends-foes-list');
                if (n >= 0) {
                    strFriends = strFriends.substr(n+30);
                    nEnd = strFriends.indexOf('">') ;
                    FriendsID = FriendsID + "," +  strFriends.substr(0,nEnd);
                    strFriends = strFriends.substr(nEnd+2);
                    nEnd = strFriends.indexOf('</a>') ;
                    Friends = Friends + "," +  strFriends.substr(0,nEnd);
                }
            }
            FriendsID = FriendsID.substr(1);
            Friends = Friends.substr(1);
            n = 0 ;
            nEnd = 0;
            var Foes = "";
            var FoesID = "";
            while (n >= 0) {
                n= strFoes.indexOf('friends-foes-list');
                if (n >= 0) {
                    strFoes = strFoes.substr(n+30);
                    nEnd = strFoes.indexOf('">') ;
                    FoesID = FoesID + "," +  strFoes.substr(0,nEnd);
                    strFoes= strFoes.substr(nEnd+2);
                    nEnd = strFoes.indexOf('</a>') ;
                    Foes = Foes + "," +  strFoes.substr(0,nEnd);
                }
            }
            FoesID = FoesID.substr(1);
            Foes = Foes.substr(1);
         
          foesID = FoesID;
         
        });
        return foesID //return after ajax request
    }



    but still use
    Code:
     foesID
    to return string
    Ch@lo Valdez
    Ch@lo Valdez
    Forumember


    Male Posts : 138
    Reputation : 50
    Language : spanish

    Solved Re: Return Value

    Post by Ch@lo Valdez September 16th 2016, 9:09 am

    ok after read a lot of pages this is how, i think Very Happy

    Code:

    function getFoesList(uID) {
        var B, A = $.ajax({
            type: "GET",
            url: '/u' + uID + '/profile?mode=editprofile&page_profil=friendsfoes',
            async: false,
            cache: false,
            type: "get",
            dataType: "text",
            success: function (response, status, xhr) {
                if (xhr.status == 200) {
                    var n2 = response.indexOf('>Foes list<'),
                        n1 = response.indexOf('>Friends list<'),
                        strFoes = response.substr(n2),
                        strFriends = response.substr(n1, n2 - n1),
                        n = 0,
                        nEnd = 0,
                        Friends = "",
                        FriendsID = "";
                    while (n >= 0) {
                        n = strFriends.indexOf('friends-foes-list');
                        if (n >= 0) {
                            strFriends = strFriends.substr(n + 30);
                            nEnd = strFriends.indexOf('">');
                            FriendsID = FriendsID + "," + strFriends.substr(0, nEnd);
                            strFriends = strFriends.substr(nEnd + 2);
                            nEnd = strFriends.indexOf('</a>');
                            Friends = Friends + "," + strFriends.substr(0, nEnd);
                        }
                    }
                    FriendsID = FriendsID.substr(1);
                    Friends = Friends.substr(1);
                    n = 0;
                    nEnd = 0;
                    var Foes = "";
                    var FoesID = "";
                    while (n >= 0) {
                        n = strFoes.indexOf('friends-foes-list');
                        if (n >= 0) {
                            strFoes = strFoes.substr(n + 30);
                            nEnd = strFoes.indexOf('">');
                            FoesID = FoesID + "," + strFoes.substr(0, nEnd);
                            strFoes = strFoes.substr(nEnd + 2);
                            nEnd = strFoes.indexOf('</a>');
                            Foes = Foes + "," + strFoes.substr(0, nEnd);
                        }
                    }
                    FoesID = FoesID.substr(1);
                    Foes = Foes.substr(1);
                    B = FriendsID; // or FoesID
                }
            }
        });
        return B
    }

    call with
    Code:
    getFoesList(_userdata.user_id);

    $.get is a shorthand of $.ajax, but by default asinc mode is true, and we need asinc in false mode
    TamDonCo
    TamDonCo
    Forumember


    Posts : 427
    Reputation : 2
    Language : English

    Solved Re: Return Value

    Post by TamDonCo September 16th 2016, 3:46 pm

    Thank You very much , Ch@lo Valdez

    You are the best ... Return Value 1f44d
    SLGray
    SLGray
    Administrator
    Administrator


    Male Posts : 51499
    Reputation : 3523
    Language : English
    Location : United States

    Solved Re: Return Value

    Post by SLGray September 16th 2016, 9:50 pm

    Problem solved & topic archived.
    Please read our forum rules:  ESF General Rules



    Return Value Slgray10

    When your topic has been solved, ensure you mark the topic solved.
    Never post your email in public.