title: Javascript: Using while loop to wait for document load not working
body: <p>I'm trying to create an html document for my coworkers to complete an online test (I have the right answers). When the document loads, it requests your user and password and tries to log in, then fill the answers and submit.</p>
  
  <p>However I can't pass the user and password values to the elements in the page, this is the error: </p>
  
  <p><code>Cannot set property 'value' of null</code></p>
  
  <p>The page is not loading completely when my script tries to get its elements, how could I make this work?</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;BCG test&lt;/title&gt;
      &lt;script type="text/javascript"&gt;
          function bcg(){
              var user = prompt("Enter username: ");
              var pass = prompt("Enter password: ");
              var site = "http://app.huawei.com/bcg";
  
              location = site;
  
              while(true){
                  if (document.readyState == "complete"){
                      break;
                  }
              }
  
              var user_box = document.getElementById("uid");
              var pass_box = document.getElementById("password");
              var submit_button = document.getElementsByName("Submit")[0];
  
              user_box.value = user;
              pass_box.value = pass;
              submit_button.click();
          }
  
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body onload = "bcg()"&gt;
      &lt;h1&gt;BCG TEST&lt;/h1&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>