Skip to content Skip to sidebar Skip to footer

Reading A Txt File From Server, Writing It To Website

Here's my problem. 1) I'm wanting to create a txt file (or possibly a html file) to hold a paragraph of information to be written onto a when a user clicks a word. It's part of a

Solution 1:

Try this, it just appends the text to the HTML of the body element:

function getFileFromServer(url, doneCallback) {
    var xhr;

    xhr = new XMLHttpRequest();
    xhr.onreadystatechange = handleStateChange();
    xhr.open("GET", url, true);
    xhr.send();

    function handleStateChange() {
        if (xhr.readyState === 4) {
            doneCallback(xhr.status == 200 ? xhr.responseText : null);
        }
    }
}

function ajax() {
    getFileFromServer("Test.txt", function (text) {
        if (text === null) {
            // An error occurred
        } else {
            document.body.innerHTML += test;
        }
    });

}

Post a Comment for "Reading A Txt File From Server, Writing It To Website"