Alex Rivera | Logout

load an HTML string into jQuery without requesting images

Asked 2012-06-12T23:20:54.883
10

Is there a way to load an HTML string that contains image tags into jQuery without requesting the images? I want to be able to run selectors on the jQuery object to extract some information. Taking the following example:

var string = $('<p><img src="http://image.url/file.jpg" /></p>');
string.find('img');

The browser will make a request for http://image.url/file.jpg. I'm trying to find a way to do the same but without the browser making a request for the image.

Thanks

Edit
Report

1 Answer

3

You need to be clear about the boundaries of your question. If your question is: "Is there any way you can load that exact HTML (without changing it) into a jQuery object (like you are doing) without causing the images to load", then the answer is NO. jQuery creates a temporary parent object and assigns your HTML to the innerHTML property which causes the browser to fully parse this HTML which WILL load the image URLs.

So, if you want to run selector operations on this HTML without loading the images, you have several options:

  1. Modify the image tags before giving the HTML to jQuery so their .src properties are gone or are empty or the <img> tags aren't image tags or are no longer present.

  2. Use something other than this type of jQuery to parse your HTML to prepare it for selector operations.

If you disclosed a little more about why you're trying to do this or what you're trying to do with the selector operations, we could perhaps suggest more alternatives.

answered 2012-06-12T23:24:51.090

Your Answer