Link to home
Start Free TrialLog in
Avatar of Ahmet Ekrem SABAN
Ahmet Ekrem SABANFlag for Austria

asked on

Problems with dynamic loading in JavaScript

I am a JavaScript newbie and learn by working on a pure JavaScript "project" that calculates mathematical functions. It all works well. Now, as a further step, I want to make the messaging multilingual. The code should be capable of loading the appropriate language file at runtime. For the dynamic loading issue, I read and found solutions on Web pages like this one.

Before writing the dynamic code, I loaded it statically and the test code worked well. The code I am asking for help about is just making the minor difference of loading a "script" element.

The code where I run into problems is the this.getString function, where it is not possible to access the de element in the language file. At line console.log(eval(language, tag));, I get the error message "Uncaught ReferenceError: de is not defined".

Thank you for your help!
Utils.zip
Multilingual.zip
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ahmet Ekrem SABAN

ASKER

Thank you very much for your answer & sorry for the missing HTML files that I obviously forgot to add. The code is split in order to learn to work with "packages". I asked the same question on stack overflow & got a good hint there, which helped me to solve the problem. By the way: The double eval is part of code I found online & not a good idea, but it works.

Thank you for your help!
As I said, if you are using ANY javascript eval( ) , then you can use much better JS operations to do your work, and have a better understanding of using javascript methods. The code you use in the -

function Language(language) {

is very awkward, some woud even say bad,  and you do not seem to know that you can assign Objects to a common variable, and get a change of the Object used, without using any eval( ).

also in your language JS files, you have something like-
    de = { pleaseWait : "some text here"}

if you declare a global JS variable like -
    var dynamic = {};

then instead in your language JS files you can use
    dynamic.de = { pleaseWait : "some text here" }

and then access the  "de" contents in your Language object
   var lan = new Language("de") ;

with the method
   lan.getString('pleaseWait');

and use -
    var strReturn = dynamic[language][tag];

no eval( ) is needed.