incorrectly removed the defined variable

Jan 9, 2013 at 8:04 AM
Edited Jan 9, 2013 at 8:06 AM

I downloaded the latest AjaxMinifier (4.78) but it incorrectly removed the defined variable.

the current javascript:


function (containerId, isDirtyHiddenFieldId)
            eval(dsName + '.clear();');
            if (isDirtyHiddenFieldId)
                objStore.isDirtyHiddenFieldId = isDirtyHiddenFieldId;
            var objContainer = document.getElementById(containerId);
            eval('attachEvent( objContainer, "mousedown", ' + dsName + '.initializeElement);');
            eval('attachEvent( objContainer, "keydown", ' + dsName + '.initializeElement);');



The minified javascript by Ajax Minifier:


function(n,r){eval(i+".clear();"),r&&(t.isDirtyHiddenFieldId=r);var u=document.getElementById(n);eval('attachEvent( objContainer, "mousedown", '+i+".initializeElement);"),eval('attachEvent( objContainer, "keydown", '+i+".initializeElement);")}


the objContainer variable was been  removed incorrectly.

How to solve this problem?

Jan 9, 2013 at 9:46 AM

I have solved the question.

add EvalTreatment:

var cs = new CodeSettings();
cs.EvalTreatment = EvalTreatment.MakeAllSafe;

Jan 12, 2013 at 1:40 AM

Correct. It didn't remove the variable, it renamed it. Big difference. Saying your evals are not safe but instead need to be made safe via that exact setting you mention is one way to get around that issue. Of course, that setting will cause A LOT of variables to not be renamed, depending on the structure of your source code. You might want to try MakeImmediateSafe to see if that fixes the issue without renaming variables outside the function. And if objContainer is the only variable you need to not be renamed, you should just put a string like this inside your function there: "objContainer:nomunge"; to tell the minifier to not rename a variable named "objContainer" inside your function, thereby fixing the issue.

Now that said, evals are REALLY BAD, and that code is horribly inefficient. So can I ask why you are use a string for dsName and not an actual object reference? If you just have to use a name and not an object, if it's the name of a global object then you can replace the first eval with window[dsName].clear(), and the last two evals with attachEvent(objContainer, "mousedown", window[dsName].initializeElement);attachEvent(objContainer,"keydown",window[dsName].initializeElement). If it's the name of a var somewhere in the scope stack and you can't just pass the variable itself (maybe because the name can point to any one of n possible variables), then you could use an object literal to map the objects to names and reference them that way. There are many, many ways in JavaScript to get to various objects, and eval is just about the worst way possible. The only valid reason I have ever seen for an eval is to process validated JSON data in down-level browsers that don't support the JSON object. I have never seen an implementation that uses eval that couldn't be written in a better, more maintainable, and more performance-minded manner. Just sayin'.