views:

1141

answers:

5

Hello! 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?

+1  A: 

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
oops, sorry, thats was just a typo! edited the question now
blmstr
i will check that, thanks
blmstr
A: 

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
oops, sorry, thats was just a typo! edited the question now
blmstr
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
it feels like im getting different results from my tests all the time. i will create a test-page.
blmstr
@users_1313: You should edit your answer to the one you gave in the comment. Maybe you can get credit as THE answer..
awe
Okay, will do, thanks :)
n1313
I've also tested not locally, same problem. I also use web developer-addon to disable cache, so they shouldnt be cached
blmstr
I just tested to add a timestamp to the filename so it never get cached: http://dev.bolmaster2.com/dev/lazyload-test/nocache.phpthis works as excepted, so my problem was that i trusted web developer-firefox-plugin to disable the cache but it didnt. Thanks!
blmstr
So was your question answered?
fudgey
A: 

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
oops, sorry, thats was just a typo!
blmstr
+1  A: 

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
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
blmstr
A: 

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

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

Slaggg
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.
blmstr