title: load specific image before anything else
body: <p>I'm a creating a loading screen for website I am making. The website loads many images, scripts, etc. The HTML and CSS part is great, but I need a way to guarantee that the "loading..." image will be loaded before anything else.</p>
  
  <p>I'm using jQuery, and everything is initiated within <code>$(function () { ... });</code>. I imagine that the code for this would need to be called before/outside that block, and the code to remove the loading screen will be called at the very end of that block. Currently, the loading image is set as a DIV background, which is the way I prefer it. However, if it's completely necessary, I will settle for an IMG tag.</p>
  
  <p><strong>Update: (solution)</strong></p>
  
  <p>I was able to answer my own question by using a combination of Robin and Vlad's responses. Both were very good, and excellent answers, however the problem is that they were aimed to load an image before another image, rather than load an image before anything else. (CSS, JS, etc...)</p>
  
  <p>Here's the dirty version of what I came up with:</p>
  
  <pre><code>var files = [new Image(), document.createElement('link'), document.createElement('script')];
  files[0].setAttribute('src', 'images/loading.gif');
  files[1].setAttribute('rel', 'stylesheet');
  files[1].setAttribute('type', 'text/css');
  files[1].setAttribute('href', 'test.css');
  files[2].setAttribute('type', 'text/javascript');
  files[2].setAttribute('src', 'js/jquery-1.5.1.min.js');
  window.onload = function (e) {
      document.getElementsByTagName('head')[0].appendChild(files[1]);
      document.getElementsByTagName('head')[0].appendChild(files[2]);
  }
  </code></pre>
  
  <p>Taking a look at the load sequence on the network tab of Chrome's developer console shows that 'loading.gif' is loaded first, then 4 dummy images, then 'test.css', and then 'jquery.1.5.1.min.js'. The CSS and JS files don't begin to load, until they've been inserted into the head tag. This is exactly what I want.</p>
  
  <p>I'm predicting that I may begin to have some problems, however, when I begin to load a list of files. Chrome reports that sometimes the JS file is loaded first, but the majority of the time the CSS file is loaded first. This isn't a problem, except when I begin to add files to load, I will need to ensure that jQuery is loaded before a script file that uses jQuery.</p>
  
  <p>If anyone has a solution for this, or a way to detect when the CSS/JS files are finished loading, using this method, then please comment. Though, I'm not sure that it's going to be a problem yet. I may need to ask a new question in the future about this, if I start to run into problems.</p>
  
  <p>Thank you to every who has helped with this issue.</p>
  
  <p><strong>Update: (glitch fix)</strong></p>
  
  <p>I ended up running into a lot of problem with this method, because the script files were being loaded asynchronously. If I would clear the browser cache, and then load the page, it would finish loading my jquery dependent files first. Then if I refreshed the page, it would work, because jquery was loaded from cache. I solved this by setting up an array of files to load, then putting the load script into a function. Then I would step through each array item using this code:</p>
  
  <pre><code>element.onload = function() { 
      ++i; _step();
  }
  element.onreadystatechange = function() { 
      if (("loaded" === element.readyState || "complete" === element.readyState)) { ++i; _step(); }
  }
  </code></pre>