title: Waiting for a file to load (onload JavaScript)
body: <p>I want to read a text from a file and return it in a function. So here's the important part of my code:</p>
  
  <pre><code>        function getFileRequest(id, contentType, callback) {
          var val = "x";      
  
          if (window.File &amp;&amp; window.FileReader &amp;&amp; window.FileList &amp;&amp; window.Blob) {
              var element = document.getElementById(id);
  
              var file = element.files[0]; 
              if(file != null) {      
                  if(file.type.match("text/xml")){
                      var r;
                      r = new FileReader();
  
                      r.onload = function (e) {
                          val = e.target.result;                    
  
                      }                   
                      r.readAsText(file);
                  }
                  else
                      alert("Wrong file format");
              }
  
          } else {
              alert('The File APIs are not fully supported by your browser.');
          }
  
          alert(val);
          if(val == null)
                  return "";
              else
                  return getRequestBody(id,contentType,val);  
  
      }
  </code></pre>
  
  <p>I want to pass the text to a variable called "val". But, as it seems to me at least, alert(val) is always showing default "x" because probably it's not waiting for onload function to be executed. Am I right at all? 
  How can I get an access to that text then? Is there a way to wait for an excecution?</p>