javascript eval() IE

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theS70RM
    New Member
    • Jul 2007
    • 107

    javascript eval() IE

    Hey,

    Im at my wits end!

    Im trying to get IE to store a string that defines a multi-demensional array as an actual array.

    Heres what the array definition would look like:
    Code:
    foo = Array(	{'key' : 'value', 'key2' : 'value2'},{'key' : 'value', 'key2' : 'value2'},	{'key' : 'value', 'key2' : 'value2'},	{'key' : 'value', 'key2' : 'value2'});
    alert(foo[1]["key"]);
    No problem!

    but i have this stored as a string (without the foo = part). So im using eval() to evaluate the code. It's worked in most combinations i've tried in firefox no problems but none in IE. Heres some of what ive tried:

    Code:
    foo = "Array(	{'key' : 'value', 'key2' : 'value2'},{'key' : 'value', 'key2' : 'value2'},	{'key' : 'value', 'key2' : 'value2'},	{'key' : 'value', 'key2' : 'value2'});";
    
    foo = "\"Array(	{\"key\" : \"value\", \"key2\" : \"value2\"},{\"key\" : \"value\", \"key2\" : \"value2\"},	{\"key\" : \"value\", \"key2\" : \"value2\"},	{\"key\" : \"value\", \"key2\" : \"value2\"});\";";
    
    foo = "\"Array(	{'key' : 'value', 'key2' : 'value2'},{'key' : 'value', 'key2' : 'value2'},	{'key' : 'value', 'key2' : 'value2'},	{'key' : 'value', 'key2' : 'value2'});\"";
    
    
    myarray = eval(foo);
    alert(myarray[1]["key"]);

    To further complicate things, some of the values in the array sometimes have ' characters in, however i just want to get it working without first then i can probably just add slashes to solve that problem.

    Any Help is much apprieciated!

    Andy
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    i just tested it in IE6 addressbar:

    Code:
    javascript:var a = eval("Array(   {'key' : 'value', 'key2' : 'value2'},{'key' : 'value', 'key2' : 'value2'}, {'key' : 'value', 'key2' : 'value2'},    {'key' : 'value', 'key2' : 'value2'});"); alert(a[0].key);
    that was your first showed option and it alerted 'value' as it would be expected? ... what IE version do you use? is any error shown?

    kind regards

    Comment

    • theS70RM
      New Member
      • Jul 2007
      • 107

      #3
      oh yea, should have thought about testing it like that,

      turns out it was a problem with my variable name and another class that i wrote conflicting.

      Thanks for your help!

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        no problem ;) ... post back to the forum anytime you have more questions ...

        kind regards

        Comment

        Working...