6

I am trying to stop the loading of images with javascript on dom ready and then init the loading whenever i want, a so called lazy loading of images. Something like this:


$(document).ready(function () {
  var images = $('img');
  $.each(images, function() {
    $(this).attr('src', '');
  });
});

This doesn't work (tested in ff3.5, safari 3-4). The images is getting loaded anyway, i dont get it.

For example this plugin, www.appelsiini.net/projects/lazyload, is doing the exact same thing, removing the src attribute on page load.

What am i missing?

EDIT: I added a test page here: http://dev.bolmaster2.com/dev/lazyload-test/ I first remove the src attribute completely, then after 5 seconds I add it with the original image. Still doesn't work, at least firebug says the images get loaded at start, is firebug to trust?

bolmaster2
  • 8,408
  • 4
  • 17
  • 16

9 Answers9

4

try removeAttr("src"); as in http://www.appelsiini.net/projects/lazyload

$(document).ready(function () {
  var images = $('img');
  $.each(images, function() {
    $(this).removeAttr("src");
  });
});

If it still is not working. Check this.loaded - maybe they are loaded too fast.

Eldar Djafarov
  • 19,573
  • 2
  • 31
  • 27
3

If your goal is to prevent the server from loading the images, then clearing the "src" attribute will not work when you run in at document.ready (at least not always - try downloading pinterest's html for example and addy your script to the saved html - you will see that the browser will request the images from the server before the src is cleared).

Instead you can try to use the same code (or better, without jQuery, to make sure it runs as fast as possible) by putting it in the "head" section inside a setInterval loop that will clear the "src" attribute from all images as soon as the tags are present (before jQuery document ready is fired).

Example:

remove the images without jQuery:

function removeImagesBeforeTheyAreRequested(options) {
  var images = document.getElementsByTagName('img');
  for (var i = 0; i < images.length; i++) { 
    var orgSrc = images[i].src;
    images[i].removeAttribute('src'); 
  }
}

fire this code in the "head" section, before the body is ready (basically monitor the existence of img tags):

var timer = setInterval(function() {
   removeImagesBeforeTheyAreRequested();
}, 1);

quit trying to look for images after 3 seconds:

setTimeout(function() { clearInterval(timer); }, 3000);

Note that you might want to check if images are cached before removing their "src" attribute (those would be loaded by the browser really fast and there's no point in removing their "src" for the purpose of removing load from the server as they are not requested from the server again).

orcaman
  • 5,213
  • 6
  • 43
  • 61
1

I think the problem is that you are emptying the 'img' attribute, not the 'src'.

If you are testing this on a local page, then your local images may be loading too fast. Or maybe they are taken directly from browser cache. Try checking if the image is already loaded before emptying its 'src'.

n1313
  • 18,981
  • 7
  • 27
  • 45
  • 3
    If you are testing this on a local page, then your local images may be loading too fast. Or maybe they are taken directly from browser cache. – n1313 Aug 27 '09 at 09:35
  • it feels like im getting different results from my tests all the time. i will create a test-page. – bolmaster2 Aug 27 '09 at 09:52
  • @users_1313: You should edit your answer to the one you gave in the comment. Maybe you can get credit as THE answer.. – awe Aug 27 '09 at 09:53
  • I've also tested not locally, same problem. I also use web developer-addon to disable cache, so they shouldnt be cached – bolmaster2 Aug 27 '09 at 11:03
  • I just tested to add a timestamp to the filename so it never get cached: http://dev.bolmaster2.com/dev/lazyload-test/nocache.php this works as excepted, so my problem was that i trusted web developer-firefox-plugin to disable the cache but it didnt. Thanks! – bolmaster2 Aug 27 '09 at 11:37
  • So was your question answered? – Mottie Aug 27 '09 at 20:25
1
$(document).ready(function () {
    var images = $('img');
    $.each(images, function() {
        $(this).attr('src', '');
    });
});

This won't work because jQuery is being called after the page has loaded at:

$(document).ready()

all this code would be doing is removing the src value after the image has already been called from the server.

LPL
  • 16,007
  • 6
  • 43
  • 87
Deezilla
  • 19
  • 1
  • 5
    This is not true. `$(document).ready()` triggers after the DOM has loaded, including style sheets and scripts and excluding images (what you describe is `document.body.onload` or `window.onload`). If the browser has some free spare connections and at the point where the page triggers `$(document).ready()`, it is possible that it already started requesting *some* images from the server. However, if you have a lot of uncached images, you will still be on time to remove image `src` attributes and thus preventing these images from loading. – Blaise Aug 08 '12 at 21:20
0

I don't know if you have written wrong in the question but the attribute the you should set to an empty string is "src" not "img". Try this:

$(document).ready(function () {
  var images = $('img');
  $.each(images, function() {
    $(this).attr('src', '');
  });
});
mck89
  • 17,869
  • 15
  • 83
  • 101
0

I think your problem is you're running the code within $(document).ready, which is called when the document is ready - ie. when it is fully loaded, including the images. The LazyLoad plugin you linked to doesn't use $(document).ready, and the script is placed in the header, so it runs before/while the page loads rather than afterwards.

Waggers
  • 596
  • 2
  • 14
  • 2
    That shouldnt be the problem, because $(function(){}, the one that LazyLoad plugin uses, is just a shortcut to $(document).ready, which is run when the DOM is ready, not the documents content – bolmaster2 Aug 27 '09 at 11:14
0

With JavaScript you do it like this.

var imagesArray = document.querySelectorAll('img');

for(var i=0; i<imagesArray.length; i++) {
    imagesArray[i].src = '';
}

Consider using Vanilla Javascript first.

Sanket Sahu
  • 7,958
  • 9
  • 46
  • 60
0

I'd suggest using the Jquery Lazyload plug-in for this. It does exactly what you want.

http://www.appelsiini.net/projects/lazyload

Slaggg
  • 6,021
  • 7
  • 26
  • 27
  • Of course I could do that - but this question was more about the basic thing the lazyload-plugin is doing: removing the src of the image elements and WHY it didn't work.. And after all it did work as expected it was just that the images was cached even though I had turned off the cache on the web developer extension for firefox. – bolmaster2 Jul 29 '10 at 09:56
  • ATTENTION! The Lazy Load Plugin can damage your page's Google Search Results - the subsequent images get not indexed. Better use somethin like `data-src` in combination with `onload()`, so each element registers itself. – mate64 Apr 20 '12 at 16:23
0

I had the same problem when putting the image load on the hover event on my thumbnail image, in IE it causes a "Stack Overflow" when I hover on the thumbnail image.
But it is solved now. This code saved my day:

$(document).ready(function() {
  var images = $('img');
  $.each(images, function() {
    $(this).removeAttr("src");
  });
});
j0k
  • 21,914
  • 28
  • 75
  • 84
lisa
  • 1