//God bless you!
I am searching for a JS function which does what the PHP function file() does. I want to read a file to a string or array. Is it possible with Javascript???
Thanks!
//God bless you!
I am searching for a JS function which does what the PHP function file() does. I want to read a file to a string or array. Is it possible with Javascript???
Thanks!
Copied from my post in your other thread:
Where are you looking to load your file from, is it stored locally on your computer or is it on the server?Javascript can't read local files from the users computer unless you use a form with a file input and HTML5. Even then it is a bit hit and miss as some browser do not support the required functions.
If you want to load a file form the server you can use the XMLHttpRequest object and get the responseText or responseXML, depending on the format of the data.
Once you have the data loaded in either of these ways you can use arrays and string splitting just like you would in PHP, although Javascript is considerably less reliable and fast as PHP.
The data can be split into an array with a record for each line with
var fileArray = string.split('\n')
Last edited by lemon-tree; 05-20-2010 at 11:21 AM.
this might work: http://www.rgagnon.com/jsdetails/js-0034.html
You'll want to use the 'For Netscape 6' one from that page, the other two use techniques that have been superseded and do not work on all browsers.this might work: http://www.rgagnon.com/jsdetails/js-0034.html
**Moved To Programming Help**
Regards ~ Vishal
Giving Reputation(at bottom of my post ) is the best way to encourage the person who helped you on forums.
Using that code, you'll want something like this:
That probably has a few errors, but I don't have time to check it.HTML Code:<script type="text/javascript"> var filePath = PATH_TO_FILE e.g. http://www.google.com/file.txt xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET",filePath,false); xmlhttp.send(null); var fileContent = xmlhttp.responseText; var fileArray = fileContent.split('\n') //Now do whatever you need with the array </script>
Last edited by lemon-tree; 05-20-2010 at 12:13 PM.
The file you grab with a XMLHttpRequest has to be on the same domain as the page where you are making the request.
Nothing is always absolutely so.
....unless you make an ajax request to a php file that uses curl to load in the actual file.
Easiest way by far is to use Jquery to load the content from a local php file into the dom.
The local php file that is used in the AJAX call would have the ability to load content from remote sources.
( Reason I suggest Jquery, is that you will no longer need to mess around with browser sniffing and creating different types of objects, Jquery can do it with one call, e.g $('#result').load('ajax/test.html'); )
Last edited by thanks_but_no_thanks; 05-20-2010 at 02:01 PM.
Of course. I was pointing out the "security sandbox" because one of the examples given above was a file from google.com.
There are other ways around the restriction using dynamic script tags and JSON. You can get image lists from Flickr that way.
jQuery and Dojo are the easiest ways to do a lot of Ajax work. But it is nice to slog through the low level coding once to appreciate what a good library can do.
Nothing is always absolutely so.
absolutely descalzo...
To answer your question tscrap
This can be complex to do, however if you utilise a framework such as Jquery it can be achieved with a single function call.a JS function which does what the PHP function file() does. I want to read a file to a string or array.
Firstly you will need a way to preserve the structure of data from php to javascript.
Use JSON (JavaScript Object Notation) http://en.wikipedia.org/wiki/JSON
Php supports JSON http://php.net/manual/en/ref.json.php
Jquery`s can make a single request to fetch JSON via AJAX and handle the complexitys http://api.jquery.com/jQuery.getJSON/