ilarsona
ilarsona

Reputation: 436

Error: onreadystatechange isn't a function

For debugging, I'm using Google Chrome. I don't know what I'm doing wrong in my code... all I'm doing is looking for the ready state to change. It's throwing the following error:

TypeError: Property 'onreadystatechange' of object # is not a function

Here's the code:

function startup() {
// For fetching without refresh
// Define new connection
var connect_to = new XMLHttpRequest();
// Open it with the necessary details
connect_to.open('GET', 'light.php?function_id=2');
// Send it
connect_to.send();
// Now, when it comes back, evaluate it for it's ready state and status (readyState equalling 4 and status equalling 200)
connect_to.onreadystatechange(function () {
    if (connect_to.readyState == 4 && connect_to.status == 200) {
        // Declare where this is going to be put
        var plant_into = document.getElementById('intendedContent').innerHTML;
        // Put the recieved text into it
        plant_into = connect_to.responseText;
    }
})
}

Upvotes: 0

Views: 576

Answers (1)

Mateusz
Mateusz

Reputation: 2317

It is event so you have to assign function to it, it can have multiple functions attached:

connect_to.onreadystatechange = function (){body};

This () is operator for calling functions, if you put it after something it will try to run function with such name. If you do Foo() then it will try to find Foo and if it will not find it then it will be error. So your usage of ready state changed look like you want to call method passing function to it as a parameter.

Upvotes: 1

Related Questions