I’ve been using eval in my code and I recently found out that there can be serious security issues If eval() is used inside Javascript. The most common scenario is where I’m using eval() to compose a variable name and then get the value of that variable like here;
var a = "2" // could be any value
work(a);
function work(a)
{
var l1 = "something";
var l2 = "something else";
var l3 = "something different";
alert(eval("l"+a));
// alerts "something else"
};
Are there any alternatives to eval() in a situation like this ??
I’ve tried using window[“l”+a] but that will only work if the variables were global and also document.getElementById(“l”+a) and that doesn’t work either.
Any help greatly appreciated.
Thanks,
Norman.
perhaps use an object or an array:
or, as an array: