{"id":137,"date":"2024-06-22T22:01:29","date_gmt":"2024-06-22T22:01:29","guid":{"rendered":"https:\/\/webaffair.net\/blog\/?p=137"},"modified":"2025-11-20T18:42:04","modified_gmt":"2025-11-20T18:42:04","slug":"ajax-and-images","status":"publish","type":"post","link":"https:\/\/webaffair.net\/blog\/code\/basics\/ajax-and-images\/","title":{"rendered":"AJAX and Images"},"content":{"rendered":"\n<p>Okay so I recently figured this out, fixed it on my scripts, forgot about it, was just reminded, realized I ought to document\/share. So this is getting posted here and a couple other places.<\/p>\n\n\n\n<p>When you do a fetch() or $.get() the browser only fetches that specific page, and doesn&#8217;t bring along any external\/linked files. One extra server request on the page &#8211; so far, so good, and we know to expect not to get the external javascript coming along for the ride, etc<\/p>\n\n\n\n<p>HOWEVER.<\/p>\n\n\n\n<p>The moment you use <code>new DOMParser().parseFromString(data, 'text\/html');<\/code> or <code>$(data)<\/code> it will <strong><em>pre fetch every image on that page<\/em><\/strong> even though you haven&#8217;t inserted any of them onto the visible page yet.<\/p>\n\n\n\n<p>Depending on which page you&#8217;re fetching, it could only make a trivial difference, but equally it can easily result it fetching hundreds of images, even if you don&#8217;t actually want to use any of them. Worse, that&#8217;s potentially hundreds of <em>broken<\/em> images who&#8217;s requests have to time out before the next &#8216;on page load\/document ready&#8217; javascript will execute (which is how I realized something fishy was going on).<\/p>\n\n\n\n<p>The solution is to make the images sourceless (so far as the browser is concerned) <em>before<\/em> parsing the html, and then restore the src value for any images you actually want shown.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$.get('\/?act=Members&amp;max_results=1000', function (data) {\n\u00a0 \u00a0 data = data.replace(\/src=\/g, \"data-src=\"); \/* prevent image prefetching!! *\/\n\u00a0 \u00a0 let doc = new DOMParser().parseFromString(data, 'text\/html');\n\n\u00a0 \u00a0 \/* your code *\/\n\n\u00a0 \u00a0 $(\".thing.inserted.onto.page img&#91;data-src]\").each((i,e) => { \n         e.src = e.dataset.src; \n    });\n});<\/code><\/pre>\n\n\n\n<p>(I can&#8217;t claim credit for the solution, but I&#8217;ve gone and forgotten which blog I found it on. Sorry.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Okay so I recently figured this out, fixed it on my scripts, forgot about it, was just reminded, realized I ought to document\/share. So this is getting posted here and a couple other places. When you do a fetch() or $.get() the browser only fetches that specific page, and doesn&#8217;t bring along any external\/linked files&#8230;. <a class=\"read-more\" href=\"https:\/\/webaffair.net\/blog\/code\/basics\/ajax-and-images\/\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[21],"tags":[28,30,38,29],"class_list":["post-137","post","type-post","status-publish","format-standard","hentry","category-basics","tag-ajax","tag-javascript","tag-jcink-skinning","tag-jquery"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/webaffair.net\/blog\/wp-json\/wp\/v2\/posts\/137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webaffair.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webaffair.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webaffair.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webaffair.net\/blog\/wp-json\/wp\/v2\/comments?post=137"}],"version-history":[{"count":4,"href":"https:\/\/webaffair.net\/blog\/wp-json\/wp\/v2\/posts\/137\/revisions"}],"predecessor-version":[{"id":145,"href":"https:\/\/webaffair.net\/blog\/wp-json\/wp\/v2\/posts\/137\/revisions\/145"}],"wp:attachment":[{"href":"https:\/\/webaffair.net\/blog\/wp-json\/wp\/v2\/media?parent=137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webaffair.net\/blog\/wp-json\/wp\/v2\/categories?post=137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webaffair.net\/blog\/wp-json\/wp\/v2\/tags?post=137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}