Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / December 2004



Tip: Looking for answers? Try searching our database.

function / return help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jason Morehouse - 30 Dec 2004 16:00 GMT
Hello,

PHP programmer in need of some js help.  I can't seem to get this
function to return the value of xmlhttp.responseText.
alert(xmlhttp.responseText) gives me the value I'm looking for.  I
assume this has to do scope... but don't quite get how to get the return
value in the `= function()` syntax.

function phj_func(url) {

       xmlhttp.open("GET", url)
       xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                return(xmlhttp.responseText);
            }
       }

      xmlhttp.send(null);

}

This was taken from:
http://www.webpronews.com/webdevelopment/basicdevelopment/wpn-37-20041201PHPOnTh
eFly.html


Thanks!
Jason Morehouse - 30 Dec 2004 16:24 GMT
> Hello,
>
[quoted text clipped - 3 lines]
> assume this has to do scope... but don't quite get how to get the return
> value in the `= function()` syntax.

Sorry, let me retry that...

 var r = false;
 function phj_func(url) {
        xmlhttp.open("GET", url)
        xmlhttp.onreadystatechange = function() {
             if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                 r = xmlhttp.responseText);
             }
        }
       xmlhttp.send(null);
      return r;
 }
Martin Honnen - 30 Dec 2004 16:26 GMT
> I can't seem to get this
> function to return the value of xmlhttp.responseText.
> alert(xmlhttp.responseText) gives me the value I'm looking for.  

> function phj_func(url) {
>
[quoted text clipped - 4 lines]
>             }
>        }

The onreadystatechange handler is a function of its own that is called
but XMLHttpRequest object when the readyState changes, the phj_func call
is already finished when the handler is called so obviously you can't
return any value in phj_func that stems from the onreadystatechange
event handler.
You need to call another function and pass the responseText text e.g.

       xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                functionName(xmlhttp.responseText);
            }
       }

Signature

    Martin Honnen
    http://JavaScript.FAQTs.com/

Jason Morehouse - 30 Dec 2004 16:35 GMT
> The onreadystatechange handler is a function of its own that is called
> but XMLHttpRequest object when the readyState changes, the phj_func call
> is already finished when the handler is called so obviously you can't
> return any value in phj_func that stems from the onreadystatechange
> event handler.
> You need to call another function and pass the responseText text e.g.

Cheers.  Is there a way though to have the data returned to the function
called.  Ie:

str = phj_func(url)?

Something like:

function phj_func(file,func,args) {
       var r = false;
       function got_data(str_data) {
            r = str_data;
       }
       xmlhttp.open("GET", url)
       xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                got_data(xmlhttp.responseText);
            }
       }

    xmlhttp.send(null);
    return (r);
}

though... that doesn't work.
Martin Honnen - 30 Dec 2004 17:07 GMT
>> The onreadystatechange handler is a function of its own that is called
>> but XMLHttpRequest object when the readyState changes, the phj_func
[quoted text clipped - 5 lines]
> Is there a way though to have the data returned to the function
> called.

No, as said the function call to phj_func simply sets up an
onreadystatechange event handler, then calls the send method and exits,
it is already finished before the onreadystatechange event handler is
called for the first time.

There is some sort of event based programming in PHP too, for instance
if you look at the XML parser functions using Expat, there you set up
event handlers like the element_handler and then those are called when
elements are parsed.

Signature

    Martin Honnen
    http://JavaScript.FAQTs.com/

Jason Morehouse - 30 Dec 2004 17:11 GMT
> There is some sort of event based programming in PHP too, for instance
> if you look at the XML parser functions using Expat, there you set up
> event handlers like the element_handler and then those are called when
> elements are parsed.

Ah, of course.  Thank you!
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.