2

...and I mean the MOST basic way. I have a filename, and I want to get its contents in a string called contents in a javascript funcrion in an html page. Nothing dynamic, no input fields, no onload,.. the file is called contents.txt. All I want is a javascript function to just do that: get the file contents into the string. Is there not something less than 5 lines long that does this? Firefox only. No checks, I don't care for any compatibility right now...

thanks!

Ps background: i am building a parser of some sort in JavaScript and for now I just want to test the darn thing with test data...

JJJ
  • 32,902
  • 20
  • 89
  • 102
Rusty75
  • 477
  • 7
  • 19

1 Answers1

1

This is a simple approach, place the script tag at the end of the body. This will ensure the page is fully loaded before it executes.

<script>
function done()
{
    var fileContents = this.response;
}

var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.addEventListener("load", done, false);
xmlhttp.open("GET","/contents.txt",true);
xmlhttp.send();
</script>
</body>
user2182349
  • 9,569
  • 3
  • 29
  • 41