2
\$\begingroup\$

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
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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 if file is not set, it should be part of your if 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
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.