0

I would like to display a list when a user is typping text (like autocompletion).

I load a xml with the list and when the user is typping text, a javascript function loops into the xml to find matches.

Everything is ok except on Internet Explorer where it SOMETIMES displays this error : "SCRIPT65535: Invalid calling object".

  • The first time i call the js function to loop into the xml always works but if i wait 5 seconds before calling it again, it will dispay the error.
  • If i wait less than 1 second it won't display the error.

It may be because in the loop i call the getAttribute() method... when i remove it there is no error.

Thx for any help !

Here is the code :

Ajax loading :

var ajax = {};

ajax.getXMLHttpRequest = function(){
    var xhr = null; 
    if(window.XMLHttpRequest || window.ActiveXObject){
        if(window.ActiveXObject){
            try{
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e){
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        else xhr = new XMLHttpRequest();
    }
    else return null;
    return xhr;
};

ajax.loadFile = function(callback){
    var xhr = ajax.getXMLHttpRequest();
    xhr.onreadystatechange = function(){
        if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)){
            callback(xhr.responseXML);
            xhr = null;
        }
    };
    xhr.open("GET", 'file.xml', true);
    xhr.setRequestHeader("Content-Type", "text/xml");
    xhr.send(null);
};

ajax.loadFile(callback);

Callback function :

var xml_nodes = '';

function callback(response){
    xml_nodes = response.getElementsByTagName('node');
}

Then a mouseclick or whatever triggers this function :

function buttonClick(){
    for(var i=0; i<xml_nodes.length; i++){
        var attr = xml_nodes[i].getAttribute('attr');
    }
}
1
  • Searching for similar errors: there is always information about source file and line. Do you have something similar in your error message? Do you have any error in file.xml file? Some bad formating, extra comma or similar? Commented Nov 18, 2013 at 23:45

1 Answer 1

1

This is a caching problem that only occurs in Internet Explorer. Your callback(response) function assigns the node elements to the xml_nodes variable. These nodes are a part of the response which is a part of the XMLHttpRequest, which gets disposed because you have no pointers to it.

The buttonClick function will iterate over the xml_nodes that are connected to disposed XMLHttpRequest's. And these are disposed because you have no pointers to it, and are therefore invalid objects.

A simple workaround will be caching your requests in an array. However this will result in large amounts of unwanted memory usage. You should create objects from the xml response and store them. These new objects won't have any pointers to the responseXML and are therefore valid objects.

Hope this helped, had the same problem to :)

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

Comments

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.