简体   繁体   中英

Internet explorer equivalent for loading a text file in javascript

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("");

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!");
    }
}
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');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM