Page 1 of 1

Unable to return string in JavaScript function?

Posted: Wed Sep 10, 2008 3:19 pm
by JAB Creations
This is just part of the script. If I alert the xmlHttp.responseText I can see the string just fine! But I can't seem to be able to return it.

Code: Select all

function example() {
//more code here
 
if (xmlHttp.readyState==4 || xmlHttp.readyState=='complete')
{
 return xmlHttp.responseText;
}
}
So if I call the example function it should return 'available' or 'unavailable'. Alerting does so but return does not! If I alert the function's value (which it should return) I get undefined.

This is really starting to kill my productivity today, help please! :roll:

Re: Unable to return string in JavaScript function?

Posted: Thu Sep 11, 2008 9:18 am
by stakes
Seems odd. what happens if you assign

var result = xmlHttp.responseText;

then

return result;

?

Re: Unable to return string in JavaScript function?

Posted: Thu Sep 11, 2008 9:28 am
by marcth

Code: Select all

 
var Ajax = new Object(); 
 
Ajax.request = function(method, uri, callback) { 
  var httpRequest;
  
  if(window.XMLHttpRequest) {
    httpRequest = new XMLHttpRequest();
  } else {
    try {
      httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(error) {
      try {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(error) {
        return false;
      }    
    }
  }
 
  httpRequest.onreadystatechange = function() {
    switch(httpRequest.readyState) {
      case 0: // Request created
      case 1: // Request initialised
      case 2: // Request sent
      case 3: // Response packets being received
      break;
 
      case 4: // Request complete
        var response = (httpRequest.responseText) ? httpRequest.responseText : httpRequest.responseXML.documentElement;
        callback(response);
      break;
    } 
  }
 
  httpRequest.open(method, uri, true);
 
  if(method.toUpperCase() == 'POST') {
    httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-uriencoded');
  }
  
  httpRequest.send(uri);
  
  return true;
}
 

Re: Unable to return string in JavaScript function?

Posted: Thu Sep 11, 2008 3:11 pm
by JAB Creations
Thanks marcth however I have no clue how to call that function. :| I'm going to try wrapping all the code in to a parent function though I'm interested in what you also have to say besides just code.

stakes, that is one of the many things I tried unsuccessfully.

Re: Unable to return string in JavaScript function?

Posted: Thu Sep 11, 2008 3:38 pm
by marcth
Here, I've attached a file you can use--If you want something fancier than this, you may want to consider using a JavaScript framework.

The code I posted (the code in the file) is a JavaScript object. You don't need to wrap it inside a function as it is already self-contained.

It is ridiculously easy to use. Here's an example:

Code: Select all

 
  uri = 'index.php?whatever=youWant&param1=1&param2=2';
 
  Ajax.request('GET', uri,  myCallbackFunction);
 
  myCallbackFunction = function(response) {
    alert(response);
  }
 
Does this make sense? Very few people truly understand how JavaScript classes/objects work. You also have to understand how the AJAX lifecycle works, which you can lookup on w3c schools I imagine.

Edit: Take a look at the code behind my website. The project itself my be silly, but there is a lot of very good code in there.

Re: Unable to return string in JavaScript function?

Posted: Thu Sep 11, 2008 4:13 pm
by JAB Creations
Unfortunately I'm not familiar with classes in JavaScript and don't truly understand how they are useful in PHP. After all they store information just like a function or a variable. I'll do some reading about JavaScript classes and how to interact with them. Having an example of how to interact with a class would be useful...I've actually been reading about classes for the past few minutes and will try out some example code. I'll also look at your site and try to get this to work. It's definitely something different from what I keep encountering while I search so it's what I'm going to pursue through in to tonight. Something has to work after all!

Re: Unable to return string in JavaScript function?

Posted: Thu Sep 11, 2008 5:27 pm
by JAB Creations
Yeah, I'm completely confused without a working example. No, it doesn't have to do what I need it to do, it just has to do something successfully. I don't want to alert in a class, I can do that fine already with the functions already and it's useless. The function that I'm using to validate my form however is where I keep getting undefined so if I can call the class and alert the value received by the server there then everything is absolutely golden! Once I pick up the pattern then I am typically able to reproduce it to the extent it serves the goal I'm trying to achieve.

It doesn't matter if these look like gold...I'm sitting here wondering how to execute them.

HTML element --> onblur event --> custom function if (condition) --> ajax --> return --> custom function {result} --> XHTML changes applied.

Right now I'm not sure how to call the class (I would be calling this in the above custom function). I've been trying to mess around but I'm stuck at the callback...is this a function? No wait...that looks like a class...so there are two classes? Now you're just throwing wrenches in my clock. :| KISS please, keep it simple keep it stupid...is there a bare minimal example on a tutorial page I can read? You also add a period in what I'm guessing is the class name...is this allowed?

Is this how to call the JavaScript class?

Code: Select all

alert(Ajax.request.httpRequest());
Error: Ajax.request.httpRequest is not a function
Anything is easy once you know how though I'm not seeing the glue that makes A-B-C-D-E all work together. I am seeing how it could be reusable. I learn by examples, not by length explanations. In example math books go on for pages and pages about a single problem, total waste. Show me a small table of addition or multiplication and I'll see a pattern emerge. Thanks for your help thus far!

Re: Unable to return string in JavaScript function?

Posted: Thu Sep 11, 2008 7:25 pm
by marcth
Buddy, on second thought, You need to learn the fundamentals! I've been watching your posts for the last couple weeks. Take a look at the W3C School website--read EVERYTHING on their site--study it. There is NOTHING anyone here can say that they haven't said better.

Take a look at this Javascript article; it's very interesting: Private Members in JavaScript

Re: Unable to return string in JavaScript function?

Posted: Fri Sep 12, 2008 8:10 pm
by JAB Creations
It's working perfectly now. All I had to do was change AJAX in to SJAX which required changing true to false.