0

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?

3 Answers 3

2

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?

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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"]?
0

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

1 Comment

Thanks! Seems that was the issue.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.