You need to create a closure of the variable o . You can do this by assigning a function that takes a value, returning a function that uses that value. Your example can be changed in such a way as to obtain the desired effect:
var o="before"; x = function(inner) { return function() { alert(inner); } } (o);
A more detailed description is given in the MDC article , which contains a section on the use of closures with loops.
The same method can be applied in your loop. Something like this is what you would like to do:
var fn = function(x, y) { return function() { rr_download_start(x, y); } } (i, this); link.addEventListener('click', fn ,false);
source share