Jump to content
Sign in to follow this  
CarlosTheDwarf

Read information in from a File

Recommended Posts

My problem is pretty simple - I need to use JavaScript to read text in from a text file, and dont know how to do it.I learned Java first, and to anyone else who knows Java i am basically looking for a substitute for the Scanner class.Also in JavaScript for Dummies it says something in one of the appendixes about accessing Java Packages with JavaScript, but I seem to be unable to grasp how to do it.Thanks a ton,Carlos

Share this post


Link to post
Share on other sites

The responseText from AJAX returns the text from any file.I've never heard of Javascript accessing Java packages before.First read the AJAX tutorial: http://w3schools.com/ajax/default.aspJust use AJAX and when using the open() command, put the name of the text file:...xmlHttp.open("GET","file.txt",true);xmlHttp.send(null);...The string of the text file is in the responseText property

Share this post


Link to post
Share on other sites

Note that the AJAX trick requires a server-side language. If you're trying to open a file on the user's desktop, JS cannot do that unless the document running the script also lives on the user's desktop. It's a security thang.

Share this post


Link to post
Share on other sites

---------Edit: Right, it won't take files from the user's desktop, but it will read any files in its local filesystem without relying on the server or any server-side language.---------It works on the desktop, I've done it myself. AJAX reads the output of files, whether a server side language processes it or not doesn't matter.You can try it yourself.I once tried to make a graphical MIDI file interpretation using AJAX on my desktop, but it was too hard to interpret all the bits, I think I have the guide to MIDI's information in my bookmarks somewhere.AJAX is client-side technology, I just made these files on my desktop to prove it works:file.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>Test</title><script type="text/javascript">function GetXmlHttpObject(){var xmlHttp=null;try  {  // Firefox, Opera 8.0+, Safari  xmlHttp=new XMLHttpRequest();  }catch (e)  {  // Internet Explorer  try    {    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    }  catch (e)    {    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");    }  }return xmlHttp;}window.onload=function() {xmlHttp = GetXmlHttpObject();xmlHttp.onreadystatechange = function() {  if(xmlHttp.readyState == 4) {    document.getElementById('something').innerHTML = xmlHttp.responseText;  }}xmlHttp.open("GET","file.txt",true);xmlHttp.send(null);}</script></head><body><div id="something">a</div></body></html>

file.txt

Hello World!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...