Özer Gül

JavaScript read file from url

2016 06 08 Javascript

function readTextFile(file) {

	var allText
	
    var rawFile = new XMLHttpRequest()
    rawFile.open("GET", file, false)

    rawFile.onreadystatechange = function ()  {
    	
        if(rawFile.readyState === 4) {
            if(rawFile.status === 200 || rawFile.status == 0)  {
                allText = rawFile.responseText;
                
            }
        }
    }
    rawFile.send(null);
    return allText
}

Usage

var file = readTextFile("file.txt")

Leave a Reply

Your email address will not be published. Required fields are marked *

(c) Özer Gül, css framework w3.css