I am new to javascript and am trying to read a file and display it contents on browser.
I have this code 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>
But the output on I get is :
x y 5 90 25 30 45 50 65 55 85 25
Whereas the data is in format:
x y
5 90
25 30
45 50
65 55
85 25
Two questions:
1) How do i display it in the format as above
2) As of now, this happens when I click on a button.. is there any way I can automatically read from this given file rather than clicking on a button
SO this is how my html code looks like
<input type="button" value="load file" onclick="read('data.tsv')">
I want to get rid of this "onclick" and just read the file
THanks
read_file? Change it to<textarea>or<pre>.<pre>tag, or manually add codes and<br />tags.read('data.tsv')at the end of your current js code. You probably should put that js block right before</body>to make sure the reference toread_filewill exist when it runs.if(navigator.appName.search('Microsoft')>-1), check for the feature withif(!window.XMLHttpRequest)