Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.