www.webdeveloper.com
Results 1 to 2 of 2

Thread: eval string to variable - value from dif js file

  1. #1
    Join Date
    Feb 2008
    Posts
    1

    eval string to variable - value from dif js file

    Hi,

    Could anybody please help me...

    i have 2 diffrent js file

    the 1st js file is Messages.js which includes all my variable messages that i'll be going to use for my internationalization:
    var msg1en="are you sure...?";
    var msg1ph="sigurado ka...?";

    the 2nd js file is myFunct.js where i places all my scripts functions.
    function myFunct.js(){
    var curLang="en"; // This is actually dynamic according to the language
    var str="msg1" + curLang;
    confirm(eval(str)); // < --- Nothing happens here unless i placed the variable assignment inside this function; however i need to place it from a diffrent js file because it's very long.

    http://www.webdeveloper.com/forum/im...s/confused.gif


    Please help, i'm stocked on this
    }

  2. #2
    Join Date
    Nov 2002
    Posts
    4,473
    First, eval is evil. It seldom works as expected, especially for rookies.

    You would be better off using an array.
    Code:
    var msg1 = new Array();
    msg1["en"]="are you sure...?";
    msg1["ph"]="sigurado ka...?";
    ...
    function myFunct.js(){
      var curLang="en"; // This is actually dynamic according to the language
      var str = msg1[curLang];
      confirm(str);
    ...
    }
    I'm sure some JSON savvy person will chime in with the shorthand version.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles