{"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 class=\"wp-block-paragraph\">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 class=\"wp-block-paragraph\">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 class=\"wp-block-paragraph\">HOWEVER.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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 class=\"wp-block-paragraph\">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 class=\"wp-block-paragraph\">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 class=\"wp-block-paragraph\">(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_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_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":[{"id":317,"url":"https:\/\/webaffair.net\/blog\/code\/basics\/introduction-to-browser-development-tools\/","url_meta":{"origin":137,"position":0},"title":"Introduction to Browser Development Tools","author":"Jay","date":"December 18, 2025","format":false,"excerpt":"All modern desktop browsers now have integrated browser tools, however, there are subtle (and sometimes less subtle) differences between each. I find that Firefox has the best overall, though I do switch to Chrome for some specific debugging tasks. I personally only use Edge or Safari when trying to isolate\u2026","rel":"","context":"In &quot;Basics&quot;","block_context":{"text":"Basics","link":"https:\/\/webaffair.net\/blog\/category\/code\/basics\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/webaffair.net\/blog\/wp-content\/uploads\/2025\/12\/DevToolsSettingsFirefox-1-scaled.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/webaffair.net\/blog\/wp-content\/uploads\/2025\/12\/DevToolsSettingsFirefox-1-scaled.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/webaffair.net\/blog\/wp-content\/uploads\/2025\/12\/DevToolsSettingsFirefox-1-scaled.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/webaffair.net\/blog\/wp-content\/uploads\/2025\/12\/DevToolsSettingsFirefox-1-scaled.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/webaffair.net\/blog\/wp-content\/uploads\/2025\/12\/DevToolsSettingsFirefox-1-scaled.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/webaffair.net\/blog\/wp-content\/uploads\/2025\/12\/DevToolsSettingsFirefox-1-scaled.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":16,"url":"https:\/\/webaffair.net\/blog\/code\/snippets\/automatically-generate-google-fonts-link\/","url_meta":{"origin":137,"position":1},"title":"Automatically generate Google Fonts link.","author":"Jay","date":"September 17, 2021","format":false,"excerpt":"Useful if you are making a customizable skin, or if you allow users to set font preferences in their account settings. Of course, the basic way you include google fonts on your page is like this: HTML (preconnects are technically optional): <link rel=\"preconnect\" href=\"https:\/\/fonts.googleapis.com\"> <link rel=\"preconnect\" href=\"https:\/\/fonts.gstatic.com\" crossorigin> <link href=\"FONT\u2026","rel":"","context":"In &quot;Snippets and Notes&quot;","block_context":{"text":"Snippets and Notes","link":"https:\/\/webaffair.net\/blog\/category\/code\/snippets\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":293,"url":"https:\/\/webaffair.net\/blog\/portfolio\/completed\/web-design-barony-of-lions-gate\/","url_meta":{"origin":137,"position":2},"title":"Web Design: Barony of Lions Gate","author":"Jay","date":"November 22, 2025","format":false,"excerpt":"Redesigned the club's website, implemented it as a full custom WordPress theme, and migrated all the content from their previous static site. Dynamic events calendar with automatically-scheduled weekly practices, news feed pulled from their facebook page, feedback form for award recommendations, automatic backups. Not too basic, not to complicated... I\u2026","rel":"","context":"In &quot;Completed Projects&quot;","block_context":{"text":"Completed Projects","link":"https:\/\/webaffair.net\/blog\/category\/portfolio\/completed\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/webaffair.net\/blog\/wp-content\/uploads\/2025\/11\/lionsgate_website.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/webaffair.net\/blog\/wp-content\/uploads\/2025\/11\/lionsgate_website.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/webaffair.net\/blog\/wp-content\/uploads\/2025\/11\/lionsgate_website.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/webaffair.net\/blog\/wp-content\/uploads\/2025\/11\/lionsgate_website.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":115,"url":"https:\/\/webaffair.net\/blog\/code\/basics\/using-svg-filters-in-css\/","url_meta":{"origin":137,"position":3},"title":"Using SVG Filters in CSS","author":"Jay","date":"June 2, 2024","format":false,"excerpt":"Firstly, you'll need the svg itself. <svg style=\"height:0; overflow:hidden;\"> <defs> <filter id=\"wavy2\"> <feTurbulence x=\"0\" y=\"0\" baseFrequency=\"0.02\" numOctaves=\"5\" seed=\"1\"><\/feTurbulence> <feDisplacementMap in=\"SourceGraphic\" scale=\"20\"><\/feDisplacementMap> <\/filter> <filter id=\"goo\"><feGaussianBlur in=\"SourceGraphic\" stdDeviation=\"8\" result=\"blur\"><\/feGaussianBlur> <feColorMatrix in=\"blur\" mode=\"matrix\" values=\"1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 19 -9\"\u2026","rel":"","context":"In &quot;Basics&quot;","block_context":{"text":"Basics","link":"https:\/\/webaffair.net\/blog\/category\/code\/basics\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/webaffair.net\/blog\/wp-content\/uploads\/2024\/06\/filter2.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/webaffair.net\/blog\/wp-content\/uploads\/2024\/06\/filter2.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/webaffair.net\/blog\/wp-content\/uploads\/2024\/06\/filter2.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":6,"url":"https:\/\/webaffair.net\/blog\/code\/snippets\/data-binding-updates-and-sqldatasource\/","url_meta":{"origin":137,"position":4},"title":"Data Binding, Updates, and SQLDataSource","author":"Jay","date":"September 16, 2021","format":false,"excerpt":"Random Things to remember: SQLDataSource won't bind parameters properly if you don't specify a type, but the GUI data source setup window doesn't guess nor prompt you to provide them. It's under 'Advanced'; or go into the code and add them.GridView will automatically add anything with 'id' in the field\u2026","rel":"","context":"In &quot;Snippets and Notes&quot;","block_context":{"text":"Snippets and Notes","link":"https:\/\/webaffair.net\/blog\/category\/code\/snippets\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9,"url":"https:\/\/webaffair.net\/blog\/code\/basics\/funky-cursor-colours\/","url_meta":{"origin":137,"position":5},"title":"Changing the Colors of Input Elements","author":"Jay","date":"September 16, 2021","format":false,"excerpt":"Ever been on a forum site with a really nice dark theme, but then you go to make a post, and are presented with a large blindingly white text area to type in? Maybe with tiny, hard to read font? Of course you have. But this is a 100% fixable\u2026","rel":"","context":"In &quot;Basics&quot;","block_context":{"text":"Basics","link":"https:\/\/webaffair.net\/blog\/category\/code\/basics\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"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}]}}