Sunday, October 24, 2010

passing arguments to evaluated javascript (so you got a string to execute...)

var a = "hello";
var b = " world";


var func = "function(param1, param2) { document.write(param1 + param2); document.write('<br />'); return 42; }";

document.write(eval(" ("+func+")(a, b) ")); // this one does it anonymously, not garbaging context with temp functions
document.write('<br />'); 


var func2 = "function sub(param1, param2) { document.write(param1 + param2); document.write('<br />'); return 42; }";
onEvent(a, b);

function onEvent(e, v) {
     var ff = eval(func2);
     sub.apply(this, arguments); // this one uses args explicitely
}

No comments:

Post a Comment