开发者

Javascript, yet another "too much recursion" issue: Revenge of evil eval()

I'm working on a project (a very old one who was revived) for a client.

When I browse one of the JSPs of the project in Firefox (4.0 in this case, but I suppose it happens with 3.x also), the following error appears on Firebug console:

Error: too much recursion

Archivo fuente: https://localhost:9443/ClientProjectWeb/js/Object.js

Línea: 226

Here is the code snippet of that line:

if(gControl){giveFocusToControl(gControl.name);}

This is the invoked function:

function giveFocusToControl(nombreControl){
    var forma = document.forms[0];
    var campo = null;
    if(forma){
        var elemento = eval("forma."+nombreControl);
        if(eval(elemento.length)){
            if(elemento.type == "select-one" || elemento.type == "select-multiple"){
                campo = elemento;
            }else{
                campo = elemento[0];
            }
        }else{
            campo = elemento;
        }

        if(campo.style && campo.style.visibility){
            if(campo.style.visibility != "hidden"){
                campo.focus();  
            }
        }else{
            campo.focus();          
        }
    }
}

I suspect this has to do with eval() calls inside a function, but despite I have tried to replace those "evil" calls with [] alternative proposed in this document, for example: var elemento = forma[nombreControl];, I haven't got the expected result, the error still is there: same line, same message.

Please do you have any suggestio开发者_如何学编程n? Thanks in advance.


The campo.focus() is where I would start my investigation. I would put an alert just before each all to campo.focus() and check if the alert gets called multiple times. If this is the case you will need to consider where the giveFocusToContol() method is being called. The eval calls in this method are not really necessary. Without playing with the code I would say the first eval could be replaced with forma.controls[nombeControl]; I would definately have to play with the second call to get the best way to evaluate it. The purpose of the if(eval(elemento.length)) is basically a test to see if the control in question is a list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜