title: JavaScript Argument not passed across handler
body: <p>I am using Google Cloud Channels in Android's WebView, but the same problem probably occurs as well when using any socket in a similar way.</p>
  
  <p>Problem: The argument is not passed on by the handler, possibly because functions are called in a different scope.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;script src='{{ channelurl }}jsapi'&gt;&lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;script type="text/javascript"&gt;
  
              onMessage = function(message) {
  
              };
  
              var token = '{{ token }}';
              var channel = new goog.appengine.Channel(token);
  
              var handler = {
                  'onmessage': onMessage,
              };
  
              var socket = channel.open(handler);
  
              socket.onmessage = onMessage;
  
          &lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>onMessage has a single argument (string) and my onMessage function is properly called, but the argument is 'undefined', probably because it is in a different scope.</p>
  
  <p>This question might be a duplicate of these or other similar questions, and I tried to apply the recipes given there, but no success.</p>
  
  <p><a href="https://stackoverflow.com/questions/24783099/how-do-i-pass-arguments-to-an-event-handler">How do I pass arguments to an event handler?</a>
  <a href="https://stackoverflow.com/questions/16404327/how-to-pass-event-as-argument-to-an-inline-event-handler-in-javascript">How to pass event as argument to an inline event handler in JavaScript?</a></p>
  
  <p>I actually applied the code from here</p>
  
  <p><a href="http://2cor214.blogspot.com/2010/08/passing-arguments-to-event-handler-in.html" rel="nofollow noreferrer">http://2cor214.blogspot.com/2010/08/passing-arguments-to-event-handler-in.html</a></p>
  
  <p>and tried to play with things like this:</p>
  
  <p>socket.onmessage = (function (message) {return onMessage})(message)</p>
  
  <p>in many variants, but couldn't get it to work.</p>
  
  <p>I admit I am not normally developing JavaScript and I don't exactly understand what JavaScript does here, but it seems to me that the argument needs to be extracted the function wrapped somehow.</p>
  
  <p>Can anybody shed light please.</p>
  
  <p>--</p>
  
  <p>I removed parts of my code for brevity.</p>