views:

362

answers:

3

Hello,

I got an array which I iterate over and try to create a variable.

The name of the variable is variable and comes from the array. So I am using eval (this code will only be used on my local computer) to create the variables. Weird enough I can create a variable and add plain text to the contents of it. But whenever I try to set a variable variable I get nothing.

I'm also using Prototype to easily walk the DOM.

var arr_entries = some_DOM_element;

arr_entries_array = new Array();
arr_entries_array[0] = new Array();
arr_entries_array[0][0] = 'name_dd';
arr_entries_array[0][1] = arr_entries.next(13).down().next(1).innerHTML;

arr_entries_array[1] = new Array();
arr_entries_array[1][0] = 'name_pl';
arr_entries_array[1][1] = arr_entries.next(14).down().next().innerHTML;

arr_entries_array[2] = new Array();
arr_entries_array[2][0] = 'name_pm';
arr_entries_array[2][1] = arr_entries.next(15).down().next().innerHTML;

arr_entries_array[3] = new Array();
arr_entries_array[3][0] = 'name_hd';
arr_entries_array[3][1] = arr_entries.next(17).down().next().innerHTML;

arr_entries_array[4] = new Array();
arr_entries_array[4][0] = 'name_sr';
arr_entries_array[4][1] = arr_entries.next(16).down().next().innerHTML;

for(e = 0; e < arr_entries_array.length; e++)
{
    eval('var arr_entry_' + arr_entries_array[e][0] + ';');

    eval('arr_entry_' + arr_entries_array[e][0] + ' = \'' + arr_entries_array[e][1] + '\';');
}

I can alert(arr_entries_array[e][1]) just fine. I can also replace it with plain text, alert the variable afterwards and it will work.

The second eval line is where it goes wrong, any comments?

A: 

I recommend finding another method. Even though you are running the code only on your own computer and not exposing security violations, from eval, to the public eval is still faulty and extremely inefficient. I recommend that you never use eval because it can be unpredictable and because any other solution likely to be more efficient to process.

A: 

Depending on what the innerHTML values of those Dom elements are, you may be getting syntax errors inside the eval.

Try using firebug or some other debugging tool to figure out what the error message is coming from the second eval and you will probably find your culprit.

My money is on new lines (\n) being the issue.

To fix the problem, you need to properly escape the values being passed into eval such that they are literal strings which you appear to use them as inside the eval statement. e.g.

var text = "' quotes can be tricky"
eval("var variable = '" + text + "';"); //syntax error
eval("var variable = '" + text.replace(/'/g, "\'") + "';"); //works
var text2 = "\n new lines also"; //this may not be 100% correct, you get the idea
eval("var variable = '" + text2 + "';"); //another syntax error
eval("var variable = '" + text2.replace(/\n/g, "\\n") + "';"); //works
barkmadley
Thanks! Seems that was the issue.
richard
+2  A: 

Why not just set properties on Object?

If you find yourself writing code in your code and then executing it with eval() you are almost certainly going about things incorrectly. It's slow, hard to read, and introduces security holes.

JavaScript objects can have any properties you want. Why not just v = new Object(); v['name_dd'] = whatever...;, or something like that?

DigitalRoss
I will try that as well, thanks
richard
I can't seem to find the right way of implementing this in the example. I find myself creating arrays and doing exactly the same thing with objects. I still need to create the variable name, any tips?
richard
But why do you need to create a variable with a new name? If I say `dynamic_vars = new Object();`, then how is having `newname` dynamically created any better than just using `dynamic_vars["newname"]`?
DigitalRoss