Forum Moderators: open
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.
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.
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?
...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?
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?
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?
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!
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.