title: How to use onMessage inside addEventListener?
body: <p>I save the value of a <code>textarea id="save"</code> inside <code>addEventListener</code>. Then, I send it to the server with <code>xhr</code> and at the same time I open a channel by using Google App Engine Channel API, then I am trying to capture the message sent back with <code>onMessage</code>.</p>
  
  <p>Everything works, except the returned message. I understand that the returned message will be <code>evt.data</code> but it is not logged. Can you help me understand what I am doing wrong? This is a follow up to my <a href="https://stackoverflow.com/questions/7939633/how-can-i-send-data-to-chrome-extension">previous question</a>. Thanks!</p>
  
  <pre><code>document.getElementById("save").addEventListener
  (
      "click", 
      function ()
      {
          var userEmail = document.getElementById("getEmail").value;
          var formData = new FormData();
          formData.append("extension_user", userEmail);
  
          var channel;
          var socket;
          var handler = 
          {
              //I changed this to "onmessage" as draevor's answer but 
              //I still don't see "evt.data" logged
              onMessage: function (evt)
              {
                  //evt.data will be what the server sends in channel.send_message
                  console.log("evt.data received from authhandler: " + evt.data);
                  alert("evt.data is: " + evt.data)
              }
          };    
  
          var xhr = new XMLHttpRequest();
          xhr.onReadyStateChange = function()
          {   
              //this alert does not trigger
              alert("xhr.onReadyStateChange")
              if (xhr.readyState == 4 &amp;&amp; xhr.status == 200)
              {
                  token = xhr.responseText;
                  //this alert does not trigger
                  alert("token: " + token)
                  channel = new goog.appengine.Channel(token);
                  socket = channel.open(handler);
              }
          };
          xhr.open("POST", "http://ting-1.appspot.com/authsender", true);
          xhr.send(formData);
          console.log("formData sent to authsender: " + formData);
  
      }, false
  )
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Update as suggested by <a href="https://stackoverflow.com/questions/7960751/how-to-use-onmessage-inside-addeventlistener/7960918#7960918">draevor's answer</a> I added other properties of <code>onmessage</code>. I followed <a href="https://stackoverflow.com/questions/7522181/gae-channel-api-connection-doesnt-open-on-android-browser">this question</a> although I am not sure why he puts the properties in single quotes.</p>
  
  <pre><code>document.getElementById("save").addEventListener
  (
      "click", 
      function ()
      {
          var userEmail = document.getElementById("getEmail").value;
          var formData = new FormData();
          formData.append("extension_user", userEmail);
  
          var channel;
          var socket;
          var handler = 
          {
              onopen: onOpen,
              onmessage: onMessage,
              onerror: onError,
              onclose: onClose
          };    
  
          var xhr = new XMLHttpRequest();
          xhr.onReadyStateChange = function()
          {
              //this alert does not trigger
              alert("xhr.onReadyStateChange")
              if (xhr.readyState == 4 &amp;&amp; xhr.status == 200)
              {
                  token = xhr.responseText;
                  //this alert does not trigger
                  alert("token: " + token)
                  channel = new goog.appengine.Channel(token);
                  socket = channel.open(handler);
              }
          };
          xhr.open("POST", "http://ting-1.appspot.com/authsender", true);
          xhr.send(formData);
          console.log("formData sent to authsender: " + formData);
      }, false
  )
  onMessage = 
  function (evt)
  {
      //evt.data will be what the server sends in channel.send_message
      console.log("evt.data received from authhandler: " + evt.data);
      alert("evt.data is: " + evt.data)
  }
  
  onOpen =
  function ()
  {
      alert("onOpen")
  }
  
  onError =
  function ()
  {
      alert("onError")
  }
  
  onClose =
  function ()
  {
      alert("onClose")
  }
  </code></pre>