Ajax

Ola pessoal, me estudei sobre o assunto, ja faço isto funcionar mas eu não entendi direito uma coisa:

Em que momento a função processReqChange() é chamada?
É nas linhas: req.onreadystatechange = processReqChange;?

Isto me deixou confuso prq não tem o () e a função não retorna nada!
Alguem poderia me ajudar?

var req;



function loadXMLDoc(url) 
{ 
    req = null; 
    // Procura por um objeto nativo (Mozilla/Safari) 
    if (window.XMLHttpRequest) { 
        req = new XMLHttpRequest(); 
        req.onreadystatechange = processReqChange; 
        req.open("GET", url, true); 
        req.send(null); 
    // Procura por uma versão ActiveX (IE) 
    } else if (window.ActiveXObject) { 
        req = new ActiveXObject("Microsoft.XMLHTTP"); 
        if (req) { 
            req.onreadystatechange = processReqChange; 
            req.open("GET", url, true); 
            req.send(); 
        } 
    } 
} 

function processReqChange() 
{ 
    // apenas quando o estado for "completado" 
    if (req.readyState == 4) { 
        // apenas se o servidor retornar "OK" 
        if (req.status == 200) { 
            // procura pela div id="news" e insere o conteudo 
            // retornado nela, como texto HTML 
            document.getElementById('news').innerHTML = req.responseText; 
        } else { 
            alert("Houve um problema ao obter os dados:\n" + req.statusText); 
        } 
    } 
} 

function buscarNoticias()
{ 
    loadXMLDoc("http://localhost:8080/HelloServletApp"); 
} 

// funfa buscarNoticias()

Na verdade vc tá dizendo pra quando o evento readystatechange acontecer ele executar a função processReqChange.

[]'s

Functions em JavaScript sao ‘first-class citizens’, ou seja:

[code]function foo() {
return “bleah!”;
}

var bar = foo;

// imprime “bleah!”
java.lang.System.out.println(bar());

baz = function() {
return bar;
}

// imprime “bleah!”
baz()();[/code]

AE, vlw, entendi :slight_smile: