I am a beginner in JavaScript and I am attempting to read a file and display its contents on a browser.
Here is the code I have written so far:
<script type="text/javascript">
var fname ;
if(navigator.appName.search('Microsoft')>-1) { fname = new ActiveXObject('MSXML2.XMLHTTP'); }
else { fname = new XMLHttpRequest(); }
function read(filename) {
fname.open('get', filename, true);
fname.onreadystatechange= steady;
fname.send(null);
}
function steady() {
if(fname.readyState==4) {
var el = document.getElementById('read_file');
el.innerHTML = fname.responseText;
}
}
</script>
However, the output I receive is:
x y 5 90 25 30 45 50 65 55 85 25
While the data should be displayed in this format:
x y
5 90
25 30
45 50
65 55
85 25
I have two questions:
1) How can I display it in the desired format as shown above?
2) Currently, the file data is loaded when I click on a button. Is there a way to automatically read from the file without the need for clicking a button?
This is the HTML code snippet I have:
<input type="button" value="load file" onclick="read('data.tsv')">
I would like to eliminate the "onclick" event and simply have the file read automatically.
Thank you