Ahmet Ekrem SABAN
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
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,
Thank you for your help!
Utils.zip
Multilingual.zip
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
Thank you for your help!