\$\begingroup\$
\$\endgroup\$
This is a partial code of my JavaScript app.
The openDoc
function's task is:
call newDoc
if use uploads a file through fileInput
.
Or, when user drag and drop a file in the document
.
I want to review my logic and code.
function stopDefault(event) {
event.stopPropagation();
event.preventDefault();
}
function openDoc(event) {
var files = event.target.files || event.dataTransfer.files,
file = files[0],
reader = new FileReader();
if (file) { // prevent TypeError
stopDefault(event); // prevent default file drop behavior
reader.readAsText(file);
}
reader.addEventListener("load", function(event) {
newDoc(event.target.result, file.name);
});
}
fileInput.addEventListener("change", openDoc);
document.addEventListener("dragover", stopDefault);
document.addEventListener("drop", openDoc);
If you're interested, checkout the full code - https://dl.dropboxusercontent.com/u/92126558/app.js
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Mar 12, 2014 at 6:05
1 Answer 1
\$\begingroup\$
\$\endgroup\$
There is a not a lot to review, from a once over:
- Indenting is not good for
stopDefault
, but I guess that's from copy pasting - There is no sense in calling
addEventListener
iffile
is not set, it should be part of yourif
block - Personally I would first add the listener, then starting reading text
- I like your use of
stopDefault
- JsHint could find nothing wrong
- Looks easy to maintain and to grok
answered Mar 12, 2014 at 17:55
lang-html