Tuesday, June 4, 2024
 Popular · Latest · Hot · Upcoming
113
rated 0 times [  116] [ 3]  / answers: 1 / hits: 60985  / 15 Years ago, tue, november 3, 2009, 12:00:00

is there a way with javascript/jquery to prevent images from loading? I am building a slideshow from a html list with images. So I would like to collect all the src data and then prevent the images from loading. So later when the user really needs the image I would load it then.



I found some lazy loading script on google but couldn't find the way they prevent images from loading.



Thanks in advance.



Edit1:

It seems from the answers that it's not possible to use javascript to prevent images from loading.
Here is a script that does lazy loading. Could anybody explain how it works? It seems when javascript is off it just loads the images normaly and when it's on it will load them when you scroll to their location.


More From » javascript

 Answers
57

If you render the HTML on the page, even if it's hidden, it's going to load. If you want images to load only when they're needed, you're going to have to dynamically set the source (src) on the image tag in javascript.



Edit 1: The script you referenced merely checks to see how far you've scrolled the page down and then determines which images are visible (or almost visible) by checking their top -- see the $.belowthefold and $.rightoffold extensions.



The example works great when the images are all the same size because their containers can also be the same size and you won't get any odd page resizing behavior when you lazy load them. If your images' heights and widths vary, you may get some odd results.  



Edit 2:



<script type=text/javascript charset=utf-8>
$(document).ready( function() { $(img).removeAttr(src); } );
</script>

<img src=Chrysanthemum.jpg />
<img src=Desert.jpg />
<img src=Hydrangeas.jpg />
<img src=Jellyfish.jpg />
<img src=Koala.jpg />
<img src=Lighthouse.jpg />
<img src=Penguins.jpg />
<img src=Tulips.jpg />

[#98394] Thursday, October 29, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristopher

Total Points: 58
Total Questions: 103
Total Answers: 102

Location: Netherlands
Member since Thu, Jul 1, 2021
3 Years ago
;