div.addEventListener("mousemove", function(){test(event, this)}, true);
Well, of course, you get an “undefined event”! When the mousemove event is mousemove , your event handler is called:
function(){test(event, this)}
There are two ways to access the event information object. Either it is passed to the event handler as an argument, or it can be found in window.event .
Suppose that the second case holds. Since your function does not have a local variable named event , and there is no such variable in function1 that calls it, the browser looks if there is an event in the global object. In JavaScript, the global object is called window , so your function is interpreted as
function(){test(window.event, this)}
and it works.
However, as I noted earlier, in some browsers event information is passed in an argument. Therefore, your event handler would probably want to look like this:
function(event){test(event, this)}
otherwise the event passed in test() will be undefined. So, how to make a cross browser handler:
function(event) { if (!event) // ie the argument is undefined or null event = window.event; test(event, this); }
The second problem is that addEventListener() does not work in older IEs (however, in IE9). For older IEs, you should use a similar function called attachEvent() . Or, if you attach only one handler, you can do it in a simple way.
div.onmousemove = function() {...};