Old 29-01-2008, 22:45   #1 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,524
Javascript not returning true

I have a function that basically works down to this:

Code:
function someFunction(variable) { if(variable == something) { return true } else { return false }

I am using this function in the following context:

Code:
if(!functionName(variable)) { alert("function did not return true") }

Now the problem I have is that the function (someFunction) is returning true, but the 'if' (in the second piece of code I posted) statement is receiving a value of "undefined" from the function. I know for a fact the first function is returning true because I tested it in the following manner:

Code:
function someFunction(variable) { if(variable == something) { alert("before true") return true alert("after true") } else { alert("before false") return false alert("after false") }

and the result was that I got an alert window that said "before true", and no other alert windows. So the function is definitely returning, and I am under the impression that it should be a returning a value of true, yet for some reason the 'if' statement is not receiving that value from the function

Has anybody else ever encountered this before? Is it a syntax error somewhere?
  Reply With Quote
Old 29-01-2008, 23:19   #2 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,672
You're aware you're missing a bracket at the end of both functions right? Other than that it should be ok.
  Reply With Quote
Old 29-01-2008, 23:39   #3 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,524
That was just a mistype here, the brackets are in my original functions. I figured it should be true as well which is why I really cant figure out why this isn't working!
  Reply With Quote
Old 29-01-2008, 23:52   #4 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,524
Here are the actual functions I am using (with unnecessary stuff removed)

function:
Code:
function validateEmailAddress(email) { var mailAddressObject = document.getElementById(email) var XMLHttpRequestObject = createXMLHttpRequest() if(XMLHttpRequestObject) { var URL = "validate_email_address.php?email=" + encodeURI(mailAddressObject.value) XMLHttpRequestObject.open("GET", URL); XMLHttpRequestObject.onreadystatechange = function() { if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { if(XMLHttpRequestObject.responseText == "proceed") { return true; } else { return false; } } } XMLHttpRequestObject.send(null); } }

And the if statement that calls it:
Code:
if(!validateEmailAddress("email")) { proceed = false; }

with 'alert(validateEmailAddress("email"))' I get 'undefined, and with 'alert(proceed)' I get 'false'
  Reply With Quote
Old 30-01-2008, 01:22   #5 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,524
I figured it out. Kind of hard to explain the problem, but I'll try so that others can avoid this problem in the future, as it was a fairly big one.

This is how I had the structure of my script set up:

1) set an onclick listener on the submit button of my form
2) when the submit button is clicked, javascript validated the fields in the form, one of which was an email field. Two different things were checked in the email field:
2a) is there a value in the field?
2b) is the email address entered into the field a valid email address? This second check was done using an AJAX call to a PHP function that checks to see if the domain name in the email address exists. If it doesn't, a value of false was returned to the onclick listener for the submit button

This is where the problem was occurring - a value of undefined was being received by the onclick listener even though the value being returned was true (see my above scripts).

I finally realized the problem wasn't in my return value when I tested the script with a non-existent email address (haku@haku.haku) and still got a value of undefined.

The problem is in the structure of AJAX requests. Ajax requests are designed to be executed IF the readystate is 4 and the status is 200, rather than when this happens. As a result, when the script came to this point, these conditions had not yet been met, and so the script skipped past the if statement and came to the end of the function. No value was returned at the end of the function, and so the onclick listener received a value of undefined. I suppose a value was eventually returned, but by this time the onclick listener wasn't listening for it.

The solution was to start off the form validation check with the ajax request, and to include the function that called the rest of the form validation inside the ajax request. This way the form validation waits until the correct readystate and status have been reached before progressing.

What a freakin headache that was! Just cost me a whole morning, but its a mistake I will not make again in the future, and hopefully none of you will now either!
  Reply With Quote
Old 30-01-2008, 04:54   #6 (permalink)
proc355
Trailer Trash™
 
proc355's Avatar
 
Join Date: Sep 2006
Location: Glasvegas
Posts: 871
you now know the meaning of asynchronous.
__________________
meh.
  Reply With Quote
Old 30-01-2008, 05:36   #7 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,524
ha! I knew it logically before. Now I know it emotionally
  Reply With Quote
Old 30-01-2008, 06:09   #8 (permalink)
Herr Kurm
bloody peasant
 
Herr Kurm's Avatar
 
Join Date: Dec 2006
Location: Tallinn, Estonia
Posts: 2,698
This is such a load of cock.
__________________
  Reply With Quote
Old 30-01-2008, 06:19   #9 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,524
Thats what she said.
  Reply With Quote
Old 30-01-2008, 06:23   #10 (permalink)
Larixk
Senior Member
 
Larixk's Avatar
 
Join Date: Sep 2006
Location: Utrecht, Netherlands
Posts: 1,024
Send a message via MSN to Larixk
Quote:
Originally Posted by haku
Code:
function someFunction(variable) { if(variable == something) { return true } else { return false }

Code:
function someFunction(variable) { return (variable == something) }
same thing, only shorter and less dumb looking
__________________
  Reply With Quote
Old 30-01-2008, 06:33   #11 (permalink)
Shiro
shiro
 
Shiro's Avatar
 
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,524
Thanks, already switched to that on my own earlier today though.
  Reply With Quote
Old 30-01-2008, 06:42   #12 (permalink)
proc355
Trailer Trash™
 
proc355's Avatar
 
Join Date: Sep 2006
Location: Glasvegas
Posts: 871
Quote:
Originally Posted by haku
ha! I knew it logically before. Now I know it emotionally
seem to remember it bit me on the arse too (actionscript xml iirc)
__________________
meh.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8