Results 1 to 11 of 11

Thread: Javascript function return result

  1. #1

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Javascript function return result

    Hi all,

    I'm trying to make a wrapper for the XMLHttp object (ajax). Here's my function to make an ajax call:
    Code:
    function ajaxCall(url)
    {
      xmlHttp = getXmlHttObj();
      xmlHttp.open(url)
    }
    
    //usage
    var HTML = ajaxCall(...);
    //HTML of the page called is now in HTML var
    Obviously this won't work because my ajaxCall function does not return a value. But that's what I want.

    When firing open() as you know the responseText is returned in the stateChanged function, but how can I get it back to my original function, and return it into the HTML var?

    Is it possible?

    If not, can I pass a function as an argument to a function such as:
    Code:
    function ajaxCall(url, processHTML)
    {
    //call ajax, when got data - pass it to the processHTML callback
    }
    
    function processHTML(data)
    {
      //got the data returned, in data var
    }
    Cheers
    Chris

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Javascript function return result

    This maybe a stupid answer but try:

    Code:
      return ('put code here);

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: Javascript function return result

    You can have an inline function as your XmlHttpRequest callback method in which you set the value of a variable. You can also (I think) pass the method name that you want to be the callback method, so this callback method can be the one that receives the value of the HTML after which you continue your processing.

  4. #4

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Javascript function return result

    Quote Originally Posted by mendhak
    You can have an inline function as your XmlHttpRequest callback method in which you set the value of a variable. You can also (I think) pass the method name that you want to be the callback method, so this callback method can be the one that receives the value of the HTML after which you continue your processing.
    How can this be done?
    Chris

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: Javascript function return result

    Code:
    xmlHttp.onreadystatechange = SetValue;
    
    function SetValue()
    {
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
    {
    HTMLString = xmlHttp.responseBody;
    }
    }

    HTMLString was declared outside the functions at 'page' level.

    Another way to do this would be, in the called function,

    Code:
    xmlHttp.onreadystatechange = function() 
    {
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) 
          {
          if (xmlHttp.responseText) 
          {
            return request.responseText;
          }
        }
      };

  6. #6

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Javascript function return result

    Ok, I tried your second example, I didn't work, maybe I'm missing something, what was the 'request' obj? I changed it to xmlHttp.

    Then responseText gets returned to the ajaxCall function, but I then need to somehow pickup that data and return it from ajaxCall to whatever made the call.

    The only way I could get it working was a dirty way:
    Code:
    function callAjax()
    {
    
    	var xmlHttp = GetXmlHttpObject();
    	var output;
    	
    	if (xmlHttp==null)
      	{
    		alert ("Your browser does not support AJAX!");
      		return false;
    	} 
    	
    	xmlHttp.onreadystatechange = function() 
    	{
    		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) 
    		  {
    		  if (xmlHttp.responseText) 
    		  {
    			output = xmlHttp.responseText;
    		  }
    		}
    		
    	  };
    	  
    	xmlHttp.open("GET",'http://www.google.co.uk',true);
    	xmlHttp.send(null);
    	
    	while(!output)
    	{
    		// wait
    	}
    	
    	return output;	
    	
    }
    
    alert( callAjax() );
    Obviously this is unacceptable.

    How can the responseText be returned to ajaxCall, and then returned to whatever called it?
    Chris

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: Javascript function return result

    Then instead of return xmlHttp.responseText you can call MethodName(xmlHttp.responseText).

  8. #8
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Javascript function return result

    Hmm. I have the same problem. I need it like this

    Code:
    <input type="button" name="Submit32" value="Check" onClick="document.wplPart.fromPartName.value = getPartName(document.wplPart.fromPartNo.value)">
    Having a script like

    Code:
    function getPartName(partNo) { 
    	var xhr; 
    	try {
    		xhr = new ActiveXObject('Msxml2.XMLHTTP'); 
    	} catch (e) {
    		try { 
    			xhr = new ActiveXObject('Microsoft.XMLHTTP');
    		} catch (e2) {
    			try {
    				xhr = new XMLHttpRequest();
    			} catch (e3) {
    				xhr = false;
    			}
    		}
    	}
      
    	xhr.onreadystatechange = function() { 
    		if (xhr.readyState  == 4) {
    			if (xhr.status  == 200) {
    				// I need to return the value from here.
    			}
    		}
    	}; 
    
    	xhr.open("GET", "index.php?m=item&a=getPartName&partNo=" + partNo,  true); 
    	xhr.send(null);
    }
    From status 200 I need to return it so that I can use it like someTag.value = getPartName(someOtherTag.value). I can't seem to figure out how to do this. Any help? Thanks!

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: Javascript function return result

    Guys, it's a callback. You can specify a method. The method can perform your document writes or get return values, etc.

    So in nebulom's example, instead of

    document.wplPart.fromPartName.value = getPartName(document.wplPart.fromPartNo.value)

    to

    WritePartNameToTheInputField(document.wplPart.fromParNo.value, theInputFIeld);

  10. #10
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Javascript function return result

    Thanks mend, I'm doing it right now.

  11. #11
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: Javascript function return result

    It's really easy to write Object Oriented Javascript

    javascript Code:
    1. function DoSomethingWithResult(result)
    2. {
    3.     // result = responseText
    4. }
    5.  
    6.  
    7. function Ajax
    8. {
    9.     this.GetXmlHttpRequest =
    10.     function()
    11.     {
    12.         // no browser compatibility
    13.         return new XMLHttpRequest();
    14.     }
    15.  
    16.  
    17.     this.Request =
    18.     function(url, callback)
    19.     {
    20.         var xmlHttp = this.GetXmlHttpRequest();
    21.  
    22.         xmlHttp.onreadystatechange =
    23.         function()
    24.         {
    25.             if(xmlHttp.readyState == 4)
    26.             {
    27.                 if(xmlHttp.status == 200)
    28.                 {
    29.                     // call your function!
    30.                     callback(xmlHttp.responseText);
    31.                 }
    32.             }
    33.         }
    34.  
    35.         xmlHttp.open("GET", url, true);
    36.         xmlHttp.send(null);
    37.     }
    38. }


    Then the call is simple:

    javascript Code:
    1. var ajax = new Ajax();
    2.  
    3. ajax.Request("page.php?param1=this&param2=that", DoSomethingWithResult);

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