homepage Welcome to WebmasterWorld Guest from 54.196.159.11
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
javascript function result not returned but function runs
result present but not returned?
snowweb




msg:3478451
 3:48 am on Oct 16, 2007 (gmt 0)

Hi, I have a function which is called like so:


var ol = update_field(fieldtype,elem_content,record_id,null);
alert(ol); // says 'undefined'.

when I alert the result immediately before the return statement, the result is alerted perfectly here:


function update_field(fieldtype,data,record_id,orig_cont)
{
//... lots of other code here which must be working ok for the responseText to get into variable 'o'.

var o = request.responseText;
alert(o); // shows result perfectly.
return o ;
}

The function runs perfectly and obtains the expected result each time, and the script always returns, but the result in not copied into the variable 'ol'.

JSlint finds no error with the code concerning this.

Can anyone see my stupid error?!

Thanks in advance.

 

snowweb




msg:3478468
 4:17 am on Oct 16, 2007 (gmt 0)

I forgot to mention that


var ol = update_field(fieldtype,elem_content,record_id,null);
alert(ol); // says 'undefined'.

is called from within another function. Don't know if this could be a clue?

penders




msg:3478653
 10:20 am on Oct 16, 2007 (gmt 0)

Try changing your variable name to
something_else ...?!
snowweb




msg:3478676
 10:39 am on Oct 16, 2007 (gmt 0)

Thanks penders, actually I tried that earlier but just incase have just changed them to 'ducks' & 'hens' to be sure that I'm not encroaching on any reserved or special words.

Unfortunately, it didn't help. I'm baffled here, it looks like my use of functions and returns is text book, to me, but both IE, Safari and Firefox all experience the same problem.

daveVk




msg:3478702
 11:32 am on Oct 16, 2007 (gmt 0)

A long shot but try changing

var o = request.responseText;

to

var o = "" + request.responseText;

so that o is new string and not a reference to responseText object.

snowweb




msg:3478722
 11:52 am on Oct 16, 2007 (gmt 0)

Thanks dave, I tried that and it didn't work, but I'll try anything right now since I'm desperate!

One interesting point and I'm not sure if it's relevant, is that my alert in the called function update_field seems to appear AFTER the alert in the calling function appears, which should only be run after the function update_field has been called and returned it's result.

That could explain why it's empty, because it is running before the result returns from the function update_field, so why is it not waiting for update_field to run before the code continues?

penders




msg:3478751
 12:26 pm on Oct 16, 2007 (gmt 0)

...my alert in the called function update_field seems to appear AFTER the alert in the calling function appears, which should only be run after the function update_field has been called and returned it's result.... That could explain why it's empty...

It certainly could! ;) By the looks you're making some kind of AJAX request? For this you need to wait for the response from the server as your code will continue to execute before the result has come back. How are you making this call?

snowweb




msg:3478793
 1:16 pm on Oct 16, 2007 (gmt 0)

Ah, ok, not such a mystery now then! Yes, it's retrieving a list of options from the server via AJAX.

I have many functions all using the same getXMLHttpRequest function.

I can't easily move or copy the function since it's very tightly integrated with the other 1200+ lines of code.

Do you know if there is a away to get my code to wait for the function to return it's result, before continuing, without making a major change to the structure of the rest of the code?

penders




msg:3478903
 2:56 pm on Oct 16, 2007 (gmt 0)

Do you know if there is a away to get my code to wait for the function to return it's result, before continuing, without making a major change to the structure of the rest of the code?

The 3rd parameter of the open() method of the HttpRequest object specifies whether to make an asynchronous request (ie. true = AJAX) or synchronous (ie. false = not AJAX, in other words it waits, halting the browser). I'm not sure how this will effect any aschronous requests currently running....?

Although I would have thought you should be able to keep it 'AJAX' if your code is in your onreadystatechange event and executed when readyState==4 ...? Is that not how you are processing your other request?

snowweb




msg:3478920
 3:19 pm on Oct 16, 2007 (gmt 0)

Yeah, that's how I'm processing the other requests, but this one has slightly different structure to the rest as two different types of trips to the server are required. One to fetch the items for the drop down box, and the other to save the selected item, afterwards.

Your point about the 3rd parameter of the HttpRequest, could prove useful. Afterall, there will be nothing else going on at that time anyway, so that could be the answer.

It's gone 11pm now and I'm off to bed. Will let you know how I get on tinkering with the 3rd parameter tomorrow. I need to make it dynamically change from true to false, depending on the purpose of the request.

Thanks for your insight. I'd never have thought of that on my own!

snowweb




msg:3479474
 3:45 am on Oct 17, 2007 (gmt 0)

Thanks very much for your help. In the end I decided to solve the problem by duplicating the HttpRequest for the getDropDown function.

Because I was able to reduce the number of if statements I was using, the resulting code was not increased that much.

It's all working now. I wouldn't have started looking for the problem in the HttpRequest with out your guidance. Thanks.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved