0

So this is a part of a simple JavaScript code to handle xmlhttprequest. The part that is generating the error is at the bottom (else if):

    httpReq.onreadystatechange = function(){
        if (httpReq.readyState == 4) {
            if (httpReq.status == 200) {
                var strRes = httpReq.responseText;
                if(xmlOrig) strRes = (new $Xml(strRes, true)).conteudo(xmlOrig);
                if(elemDest) $id(elemDest).innerHTML = strRes;
                if(func) {
                    var dadosArray = new Array(4, strRes, httpReq, 'OK', 'Concluído com sucesso.');
                    window[func](dadosArray);
                }
            } else {
                if(elemDest) elemDest.innerHTML = 'Erro: '+httpReq.status+' | '+httpReq.statusText;
                if(func) {
                    var dadosArray = new Array(4, false, httpReq, 'erro', 'Erro, conteúdo não carregado!');
                    window[func](dadosArray);
                }
            }
        } else if(func){
            console.log("func? "+typeof(func));
            var dadosArray = new Array(httpReq.readyState);
            window[func](dadosArray);  // <-- HERE IS THE ERROR!
        }
    }

However, the console.log return the "func" argument as a function, so where is the error?

The Safari console:

func? function TypeError: 'undefined' is not a function (evaluating 'windowfunc')

3
  • Where do you define what the variable func is? You're not passing it in or defining it anywhere? Commented Jul 19, 2014 at 20:02
  • I defined a testing function in the root. First, I though it was an error inside the function, but the testing function go nothing to do (as a test). I think it can be an error that Safari can't precise where. Commented Jul 20, 2014 at 21:04
  • I found it: func must be a String, so I can't define func = Function, but func = "NameOfTheFunction". Commented Jul 20, 2014 at 21:09

2 Answers 2

1

Are you sure func is on the window? You are checking for func, which could be inside any scope, and thne you can calling window.func().

Sign up to request clarification or add additional context in comments.

2 Comments

Since typeof(func) is returning "function", I assume the function is correctly defined, and I defined it in the root.
The answer: func must be a String, so I can't define func = Function, but func = "NameOfTheFunction".
1

You probably meant to do window["func"] instead of window[func].

The latter expression is equivalent to window["function(someParam) { ... }"] (i.e., a whatever the content of func actually is). window probably does not have a property whose name is the entire stringified text content of func.

4 Comments

The function is not called "func". I'm using func = "doNothing" as a test, and function = doNothing(d){ } is defined in the root.
I found it. As long your idea is not the answer, it give me a different idea: func must be a String, so I can't define func = Function, but func = "NameOfTheFunction".
@GustavoPinent The name of the variable that holds the function is called func, and you identify that variable by name in bracket syntax by the string "func". To generalize the two cases: window["someString"] refers to the global variable someString, while window[someVar] refers to a global variable whose name is the contents of someVar. If someVar is set to "foo", then window[someVar] refers to the global foo.
Ok, but in this case, I wish to point a function to receive the responses, so window[func] will do the job (I must chose). The problem is that if func exists but doesn't has a string with the name of the function, Safari returns a generic "undefined" error. IMO Safari should return the appropriate error message, related to an argument that can't be evaluated in this context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.