JavaScript event handler question

envo

Expert Member
Joined
Jan 14, 2014
Messages
3,265
Reaction score
437
I have an event listener that fires this (after attaching it using attachEvent or addEventListener):

Code:
var handleResponse = function (e) {        
        if (e.data == "ready") {
            //some stuff here 
        }
    }

How can I get the handleResponse to accept another variable?

Something like
Code:
var handleResponse = function (e,stringhere) {        
        if (e.data == "ready") {
            //some stuff here 
        }
    }

I get an error obviously

Code:
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent"; //Cross-browser Functionality
        var eventer = window[eventMethod];
        var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message"; //Cross-browser Functionality

[b]        eventer(messageEvent, handleResponse(e,'string'), false);[/b]

I basically want to send through the string while still handling the event (e.data etc). The highlighted doesn't work

Any insights?
 
Send the parameter as an array to the function instead of a single string

i.e.

Code:
var data = { 'event': 'click', 'data': '12345' }

var handler = function ( data) {
  return function ( event ) { 

  };
};

addEventListener( "clicky", handler ( data ) );
 
Last edited:
I will try that, thanks for the suggestion
 
Top
Sign up to the MyBroadband newsletter
X