質問

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');
    }
}
役に立ちましたか?

解決

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 :)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top