How can I perform a cross-domain GET, without the browser loading referenced images?
I'm using $.get()
in an environment that allows cross-domain HTTP requests, namely, a chrome extension. I'm using it to grab a page, and scrape off information to display to the user. This works.
The only problem is that the browser attempts to load every single image r开发者_如何学Pythoneferenced in the scraped page, which both dramatically increases the data transfer, and fills up the inspector with errors when relative URLs are found.
Here's my code:
$.get('http://somewebsite.com/page-with-lots-of-images.htm', function(data) {
var thingsICareAbout = $(data).find('#some-id, #some-other-id');
foo(thingsICareAbout);
});
How can I get #some-id
and #some-other-id
wihout loading all the images in the document?
EDIT: The images are only loaded once the $(data)
call is made. Somehow, calling that on the html string triggers the images to load. Any alternative?
Why this is happening is explained here:
If the HTML is more complex than a single tag without attributes, the actual creation of the elements is handled by the browser's innerHTML mechanism. In most cases, jQuery creates a new element and sets the innerHTML property of the element to the HTML snippet that was passed in.
So when you do $(data)
it inserts all that html on your page.
How to avoid it. If something you are searching for doesn't require complex jquery selectors, maybe you can parse html yourself (for example with... regular expressions *gulp*).
Unless you're inserting this HTML into the DOM, it's just a string and I doubt the browser would start fetching images.
Before inserting it into the DOM you could do a search-replace for src="..." and replace it with something line xxx="...".
精彩评论