views:

290

answers:

2

I wish to load some additional data from javascript in an HTML page. The solution below is small and does exactly what I need to do in non-Microsoft browsers.

Question is, what is the Microsoft explorer equivalent? Note that the data I'm loading isn't in XML. I also do not wish to add a javascript library - I want this page to load fast even on dialup.


var client = new XMLHttpRequest();
client.open('GET', 'gamedata.txt');
client.onreadystatechange = function() {
    if (client.readyState == 4) {
        alert(client.responseText); // Make sure its loaded
    }
}

client.send("");
+5  A: 

Due to mixed support for XMLHttpRequest() in various versions of IE, you have to jump through a number of hoops to get the request to work. (In particular, IE5 and IE6 use an ActiveXObject for XMLHttpRequest.)

I've always used this page's fix for IE browsers:

function ajaxFunction() {
    var xmlhttp;
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else {
        alert("Your browser does not support XMLHTTP!");
    }
}
Daniel Lew
A: 
if (window.XMLHttpRequest) {
    var client = new XMLHttpRequest();
} else if(window.ActiveXObject) {
    var client = new ActiveXObject('Microsoft.XMLHTTP');
} else {
    alert('Your browser does not support XMLHttpRequest object');
}
if (typeof client.overrideMimeType != 'undefined') {
    client.overrideMimeType('text/xml');
}
Michał Rudnicki