jQuery parse HTML without loading images jQuery parse HTML without loading images ajax ajax

jQuery parse HTML without loading images


Use regex and remove all <img> tags

 html = html.replace(/<img[^>]*>/g,"");


Actually if you look in the jQuery documentation it says that you can pass the "owner document" as the second argument to $.

So what we can then do is create a virtual document so that the browser does not automatically load the images present in the supplied HTML:

var ownerDocument = document.implementation.createHTMLDocument('virtual');$(html, ownerDocument).find('.some-selector');


Using the following way to parse html will load images automatically.

var wrapper = document.createElement('div'),    html = '.....';wrapper.innerHTML = html;

If use DomParser to parse html, the images will not be loaded automatically. See https://github.com/panzi/jQuery-Parse-HTML/blob/master/jquery.parsehtml.js for details.