i have a question, is JavaScript can read an external file? i have an ear file, can it read external file without the external file compile together in the ear file? can we do so?
JavaScript is purely a client-side language, and unless used with an Ajax call has no capability to read from or write to a file (except a cookie), communicate with the server, access a database, the client's operating system or the Windows registry, or alter the default behaviour of the browser. And for security reasons JavaScript cannot access anything beyond the domain of the current page. This is known as the "same origin policy" and prevents a document or script loaded from one origin from getting or setting properties of a document from a different origin.
But an .ear file is a Java module. Do you perhaps mean Java? Even if Javascript could read it, it could not process it.
All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
JavaScript is purely a client-side language, and unless used with an Ajax call has no capability to read from or write to a file (except a cookie), communicate with the server, access a database, the client's operating system or the Windows registry, or alter the default behaviour of the browser. And for security reasons JavaScript cannot access anything beyond the domain of the current page. This is known as the "same origin policy" and prevents a document or script loaded from one origin from getting or setting properties of a document from a different origin.
This makes it sound like you are saying no, which is wrong.
I don't typically answer simple inquiries, but I believe it a disservice to OPs and lurkers to dismiss such basic questions.
File inputs and drag and drop access files on the "client's operating system", ajax, forms, <script> and Image() all "communicate with the server", registerPrototcolHandler() changes the "default behaviour of the browser" in terms of url/file handling, and the "same origin policy" has been outmoded for several browsers versions by now. Even in old browsers, jsonp and form POSTs go "beyond the domain of the current page". None of your points stand up in this day and age.
And let's not forget that the js in node.js stands for javascript; you tellin me javascript can't use files? really? Come on, give me a break; it's 2012 already...
Just because one may not know about certain technologies and techniques doesn't mean they don't exist. Now you know, so stop posting this garbage from years ago when i joined. Even with an edit or two, it's completely mis-leading on it's face. I will rebuke this malarkey 100% of the time i see such lies and rubbish propagated.
__________________ my site (updated 5/13) STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
The Following 2 Users Say Thank You to rnd me For This Useful Post:
JavaScript can read files when run on the client computer provided that the script is not running in a browser. One such place where you can easily set up client side JavaScript that can read files in in Adobe Air which has JavaScript as one of the three languages that it supports for writing applications.
I see the file contents in the <textarea>, so if JavaScript can't "read files", I must be a witch instead of a programmer.
On that page JavaScript isn't reading the file. The file is being loaded to the server and read by a server side script whick then passes the content back to JavaScript in the response to an Ajax call.
JavaScript running in the browser does not have any commands for reading or writing files.
On that page JavaScript isn't reading the file. The file is being loaded to the server and read by a server side script whick then passes the content back to JavaScript in the response to an Ajax call.
nope. no ajax. no server-side code in use.
check firebug again - no net tab activity.
I gotcha, but with good news, so rejoice.
This is code and as such, things are easy to prove; you don't have to take my word for it.
Here is an html file that can be served via http which contains a textarea into which you can drag and drop a file:
Well there is in that page but obviously (now) for some other purpose.
I hadn't come across the File API for JavaScript before (not surprising as the preliminary draft for it is dated October 2011). Probably be a while before all browsers actually support it but since it relies on user interaction to allow the file to be read it gets around the security issues associated with a web site being able to read local files.
Yes javascript can be used to read external files.
You do not need AJAX/JSON if the files are on the same server.
Simply use an iframe to read the file contents and parse as needed.
Yes javascript can be used to read external files.
You do not need AJAX/JSON if the files are on the same server.
Using an iframe like that is called Ajax. JavaScript has four different ways it can use Ajax - XMLHttpRequest, or one of the three tags - object, img or script (iframe is just an obsolete variant of object and all iframes were supposed to be replaced with object tags when HTML 4 became a standard in 1997). All four have advantages and disadvantages - for example script is the only one of the four that avoids the same domain restriction, XMLHttpRequest is the only one to trigger an event when the data is received, img is useful for passing data to the server when no response is required, object/iframe is the most limited and useless of the four.
Anyway JavaScript can't read any files from the server using any of the Ajax techniques as whichever of the four ways you write your Ajax you still need a server side script to actually read the file and convert it to a form that JavaScript can use.
It does appear though that JavaScript running in the browser can read local files that are on the same computer as the browser provided that the browser owner initiates the read request.
Just because one may not know about certain technologies and techniques doesn't mean they don't exist. Now you know, so stop posting this garbage from years ago when i joined. Even with an edit or two, it's completely mis-leading on it's face. I will rebuke this malarkey 100% of the time i see such lies and rubbish propagated.
Sorry, rndme. No need to start blowing a fuse. I phrased my response carefully and I stand by what I wrote. JavaScript running in the browser does not have any commands for reading or writing files on the server. That was the question asked by the OP. An iframe is simple HTML and involves no Javascript.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Last edited by Philip M; 02-22-2012 at 08:11 AM..
Reason: Typo
JavaScript running in the browser does not have any commands for reading or writing files
You mean apart from the recently added FileReader() object that can read files when the browser owner requests it. Currently so new that only Firefox and Chrome support it.
You mean apart from the recently added FileReader() object that can read files when the browser owner requests it. Currently so new that only Firefox and Chrome support it.
But something that works only in HTML5 and then not in IE is in my view of no practical use or value. It will be a few years before HTML4 browsers are obsolete.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
But something that works only in HTML5 and then not in IE is in my view of no practical use or value. It will be a few years before HTML4 browsers are obsolete.
It is JavaScript - NOT HTML 5. So it should be available in all browsers within the next few years - there's no reason why it wouldn't work with HTML 1 provided the browser supports the file.api. You will not have to wait 20 years (or more) for HTML 5 to be finished to be able to use it. Just because HTML 5 is now being put forward as a generic term for all the new additions to HTML, CSS, JavaScript, fast food, racing cars, egg plants, monkeys, and moon rockets doesn't mean that the term has any relevant meaning beyond HTML itself. It has as little meaning to JavaScript as it does to moon rockets - perhaps less.
This suddenly using a term for thousands of things that have nothing to do with what it was originally specific to is kind of like the way Web 2 suddenly got used for lots of things that have nothing to do with JavaScript/Ajax and Web 3 is sometimes used for things way beyond generic database tables.
That this isn't yet officially a part of JavaScript the way things like postMessage and "use strict"; are is why I am not rushing to add it to my "by example" site - I found it in David Flanagan's "JavaScript: the Definitive guide" now that I found out that it exists - had obviously ignored it before because it is still only part way through the draft process before becoming a standard (although much closer that HTML 5 is).