Categories
JavaScript

eval() is evil: window to the rescue

In JavaScript eval() has long known to be evil. Take for example:

function foo(){}
foo["str1"] = "aaaa";
foo["str2"] = "bbbb";
foo["str3"] = "ccccc";

Elsewhere you need to lookup items from this structure. Where both “foo” and “str1” are pass in as arguments. So like:

function lookUp(bundle, str) {
return eval(bundle[str]);
}

eval() will invoke a new scope and new compiler just to find and evaluate the string values. There must be a better way. Since foo is defined previously it’s available via the window object. So you can simply say:

function lookUp2(bundle, str) {
return window[bundle][str];
}

Pretty simple eh?

Categories
JavaScript

Opera 8.x XML/DOM appendChild problem

Found a problem with Opera’s XML/DOM support. Seems that they don’t support appendChild() if the document is created with an “” (empty) QualifiedName.

document.implementation.createDocument("","",null);

This only seems to be a problem if you try to append to such a document. Opera 8.x (and prior?) requires you to create the document with a QualifiedName like:

document.implementation.createDocument("","foo",null);

This way appendChild will work as expected. Of course this works fine in Firefox and IE. It also apprears to be fixed in the upcoming 9.0 release of Opera.