Jones-Rocks
Jones-Rocks

Reputation: 15

jQuery: Count images

I have a folder with images named example_**_title.jpg.

I want to count these images with jQuery/Javascript and return the number of images.

I tried:

count = 0;
for (var i=0; i<=10; i++){
   var bg_url = 'http://www.example-url.com/example_'+i+'_title.jpg';
   $.get(bg_url)
   .done(function(){ 
      count++;
   })
   .fail(function(){
   });
}
alert(count);

Thanks for the help.

Jones

Upvotes: 0

Views: 3264

Answers (2)

Starx
Starx

Reputation: 78971

Your code should work, this is a quote missing on your code. I hope that is just a typo.

count = 0;
for (var i=0; i<=10; i++){
   var bg_url = 'http://www.example-url.com/example_'+i+'_title.jpg';
   $.get(bg_url, function(data){ count++; });
}
alert(count);

Upvotes: 1

Michael Laffargue
Michael Laffargue

Reputation: 10294

Since you don't want to get the files but just count, you can use type:'HEAD' It should reduce the amount of data :

$.ajax({
    url:'http://www.example-url.com/example_'+i+'_title.jpg',
    type:'HEAD',
    error: function()
    { 
      //file doesn't exist
    },
    success: function()
    {
        count++;
    }
});

Upvotes: 2

Related Questions