You can use a technique called currying to pass the additional argument to your callback function. Basically you write an anonymous function which calls your function with the static arguments and use that as the callback function. For your example, this ought to work:
function uploadComplete (evt, attr) {
console.log( attr );
console.log( evt.target.responseText );
}
var extra = "I want to be sent as an attribute";
var xhr = new XMLHttpRequest();
xhr.addEventListener( "load", function (evt)
uploadComplete( evt, extra );
}, false );
The anonymous function is called by the event system and calls your uploadComplete
function passing both the event object it receives as a parameter and the value of extra
, which it accesses through its closure. If the extra
variable is in scope at the time you define the callback function you don't need to pass it as a argument; you can just accesses it via the callback's closure.
var extra = "I want to be sent as an attribute";
function uploadComplete (evt) {
console.log( extra );
console.log( evt.target.responseText );
}
var xhr = new XMLHttpRequest();
xhr.addEventListener( "load", uploadComplete, false );
Also note that when you pass a function to be used as a callback you just use the name of the function without parentheses. If you use parentheses you're calling the function yourself and passing its return value as the callback.