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.
Problem: The argument is not passed on by the handler, possibly because functions are called in a different scope.
Here is my code:
<html>
<head>
<script src='{{ channelurl }}jsapi'></script>
</head>
<body>
<script type="text/javascript">
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;
</script>
</body>
</html>
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.
This question might be a duplicate of these or other similar questions, and I tried to apply the recipes given there, but no success.
How do I pass arguments to an event handler? How to pass event as argument to an inline event handler in JavaScript?
I actually applied the code from here
http://2cor214.blogspot.com/2010/08/passing-arguments-to-event-handler-in.html
and tried to play with things like this:
socket.onmessage = (function (message) {return onMessage})(message)
in many variants, but couldn't get it to work.
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.
Can anybody shed light please.
--
I removed parts of my code for brevity.