function / return help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason Morehouse

    function / return help

    Hello,

    PHP programmer in need of some js help. I can't seem to get this
    function to return the value of xmlhttp.respons eText.
    alert(xmlhttp.r esponseText) 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("G ET", url)
    xmlhttp.onready statechange = function() {
    if (xmlhttp.readyS tate == 4 && xmlhttp.status == 200) {
    return(xmlhttp. responseText);
    }
    }

    xmlhttp.send(nu ll);

    }

    This was taken from:


    Thanks!
  • Martin Honnen

    #2
    Re: function / return help



    Jason Morehouse wrote:

    [color=blue]
    > I can't seem to get this
    > function to return the value of xmlhttp.respons eText.
    > alert(xmlhttp.r esponseText) gives me the value I'm looking for.[/color]
    [color=blue]
    > function phj_func(url) {
    >
    >
    > xmlhttp.open("G ET", url)
    > xmlhttp.onready statechange = function() {
    > if (xmlhttp.readyS tate == 4 && xmlhttp.status == 200) {
    > return(xmlhttp. responseText);
    > }
    > }[/color]

    The onreadystatecha nge 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 onreadystatecha nge
    event handler.
    You need to call another function and pass the responseText text e.g.

    xmlhttp.onready statechange = function() {
    if (xmlhttp.readyS tate == 4 && xmlhttp.status == 200) {
    functionName(xm lhttp.responseT ext);
    }
    }


    --

    Martin Honnen

    Comment

    • Jason Morehouse

      #3
      Re: function / return help

      Jason Morehouse wrote:[color=blue]
      > Hello,
      >
      > PHP programmer in need of some js help. I can't seem to get this
      > function to return the value of xmlhttp.respons eText.
      > alert(xmlhttp.r esponseText) 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.[/color]

      Sorry, let me retry that...

      var r = false;
      function phj_func(url) {
      xmlhttp.open("G ET", url)
      xmlhttp.onready statechange = function() {
      if (xmlhttp.readyS tate == 4 && xmlhttp.status == 200) {
      r = xmlhttp.respons eText);
      }
      }
      xmlhttp.send(nu ll);
      return r;
      }

      Comment

      • Jason Morehouse

        #4
        Re: function / return help

        Martin Honnen wrote:[color=blue]
        > The onreadystatecha nge 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 onreadystatecha nge
        > event handler.
        > You need to call another function and pass the responseText text e.g.[/color]

        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,f unc,args) {
        var r = false;
        function got_data(str_da ta) {
        r = str_data;
        }
        xmlhttp.open("G ET", url)
        xmlhttp.onready statechange = function() {
        if (xmlhttp.readyS tate == 4 && xmlhttp.status == 200) {
        got_data(xmlhtt p.responseText) ;
        }
        }

        xmlhttp.send(nu ll);
        return (r);
        }

        though... that doesn't work.

        Comment

        • Martin Honnen

          #5
          Re: function / return help



          Jason Morehouse wrote:
          [color=blue]
          > Martin Honnen wrote:
          >[color=green]
          >> The onreadystatecha nge 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
          >> onreadystatecha nge event handler.
          >> You need to call another function and pass the responseText text e.g.[/color]
          >
          >
          > Is there a way though to have the data returned to the function
          > called.[/color]

          No, as said the function call to phj_func simply sets up an
          onreadystatecha nge event handler, then calls the send method and exits,
          it is already finished before the onreadystatecha nge 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.

          --

          Martin Honnen

          Comment

          • Jason Morehouse

            #6
            Re: function / return help

            Martin Honnen wrote:
            [color=blue]
            > 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.[/color]

            Ah, of course. Thank you!

            Comment

            Working...