Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery how to call an image preloader recursively
    primarykey
    data
    text
    <p>I have a very simple jQuery preloader that works ok (i believe the code is correct but please go ahead and check it out)...<br> What i'm trying to do is, if the image fails to download, then try another variation of the image until you find one.... there's only upto 4 variations that is represented by a number inside the path.</p> <p>Let me explain:<br> An image path could be as following<br> .../images/800px/image1.jpg</p> <p>if that one fails, then try<br> .../images/350px/image1.jpg</p> <p>if that one fails, then try<br> .../images/100px/image1.jpg</p> <p>if that one fails, then try<br> .../images/50px/image1.jpg</p> <p>if that one fails, then try<br> .../images/30px/image1.jpg </p> <p>if you notice, the diference in all of those variations, is the number inside the path... that is 800, 350, 100, 50, 30 </p> <p>Now, in the function load_img (below) i was thinking of using the .error atribute to call the function recursively until it finds an image..</p> <p>IS this the best approach? any hints or best practices on how to achieve this?</p> <p>Thanks in advance<br> Marco </p> <p>Here's a sample of the code</p> <pre><code>&lt;div id="zoom-artwork"&gt;&lt;/div&gt; &lt;ul class="nav-zoom-artwork"&gt; &lt;li&gt;&lt;a href=".../images/800px/image1.jpg"&gt;image doesn't exist...&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href=".../images/800px/image2.jpg"&gt;image 2&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href=".../images/800px/image3.jpg"&gt;image 3&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; ... $('#nav-zoom-artwork a').click(function(e) { var src = $(this).attr('href'); var alt = $(this).children('img').attr('alt'); load_img(src, alt); e.preventDefault(); }); // ZOOM artwork function load_img(src, alt) { var img = new Image(); $(img).load(function() { // set the image hidden by default $(this).hide(); // remove the loading class, then insert the image $('#zoom-artwork').removeClass('loading').html(this); // fade in the image to create a nice effect $(this).fadeIn(400); }) .error(function() { alert('try another size... call the function recursively with another path...'); }) .attr({ src:src, alt:alt }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload