CarlosTheDwarf Posted June 26, 2008 Share Posted June 26, 2008 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 Link to comment Share on other sites More sharing options...
Ingolme Posted June 26, 2008 Share Posted June 26, 2008 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 Link to comment Share on other sites More sharing options...
jeffman Posted June 26, 2008 Share Posted June 26, 2008 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. Link to comment Share on other sites More sharing options...
Ingolme Posted June 26, 2008 Share Posted June 26, 2008 ---------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! Link to comment Share on other sites More sharing options...
CarlosTheDwarf Posted June 27, 2008 Author Share Posted June 27, 2008 Great thanks guys ill try it out Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.