title: Parsing invalid HTML with jQuery, without adding to DOM?
body: <p>I'm working on a jQuery plugin that uses an HTML template. End users may pass in a template that looks at the simplest level like this:</p>
  
  <pre><code>&lt;template&gt;
     &lt;img src="/images/image_{field_id}.gif"&gt;
  &lt;/template&gt;
  </code></pre>
  
  <p>The template is supposed to be commented out, so that the browser won't try to render anything, My code just extracts the interior of the comment as text, and then uses jQuery to parse it into an element tree. Then I do specific substitutions. </p>
  
  <p>I remember reading that jQuery just uses the DOM to parse HTML, e.g. when you just do </p>
  
  <pre><code>var x = $(html_string);
  </code></pre>
  
  <p>jQuery is actually just adding it to the DOM to get the elements. The simple action above would not render anything (until I actually added <code>x</code> somewhere in the DOM), but yet this code results in the browser trying to load the image target - <em>even if I never add it to the DOM.</em> Here is a simple demonstration of this effect in action: <a href="http://jsfiddle.net/b5HGU/" rel="noreferrer">http://jsfiddle.net/b5HGU/</a></p>
  
  <p>So basically - is there any way to solve this problem? Can you create an "image" element with a known bad <code>src</code> tag using jQuery, but have it not load the image until you actually add it to the real DOM?</p>
  
  <p>The other alternative I have is to complete all the parsing on the template, as a string, <em>before</em> using jQuery to construct a DOM. This is certainly doable, but in reality the template has a number of different components, and it is very convenient to use jQuery selectors to turn it into a navigable tree before outputting the parsed version. I'd have to basically write my own XML parser otherwise. Just wondering if there are any simple solutions before I do this the hard way.</p>