Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7021685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:28:59+00:00 2026-05-27T23:28:59+00:00

I had asked this question a few weeks back on how to preload images

  • 0

I had asked this question a few weeks back on how to preload images in a gallery. While the solution works, the issue that arises now is that if I click on Next to preload the next three images and there are only two images ahead, it runs the loop all three times and Firebug throws an undefined error.

This is the code:

 $('#next').bind('click',function(){
                    var $this           = $(this);
                    var $nextimage      = $('#content img:nth-child('+parseInt(current+2)+')');
                    var next = $nextimage.next();
                    for (var i = 0; i < 3; i++)
                    {
                        var img = new Image();
                        img.src = next.attr("alt");
                        next = next.next();
                    }
                    navigate($nextimage,'right');

                });

How do I catch the undefined error, that if there are only 2 or 1 images, it should preload only those images and not run the loop the third or second time to throw the error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:29:00+00:00Added an answer on May 27, 2026 at 11:29 pm
    $('#next').bind('click',function(){
                        var $this           = $(this);
                        var $nextimage      = $('#content img:nth-child('+parseInt(current+2)+')');
                        var next = $nextimage.next();
                        for (var i = 0; i < 3; i++)
                        {
                            if ( !next.length ) break; // This line stops your loop if there isn't any next
                            var img = new Image();
                            img.src = next.attr("alt");
                            next = next.next();
                        }
                        navigate($nextimage,'right');
    
                    });
    

    EDIT:

    Maybe you have next element in the DOM but not with the attribute “alt”:

    $('#next').bind('click',function() {
    
        var $this = $(this),
            $nextimage = $('#content img:nth-child('+parseInt(current+2)+')'),
            $next = $nextimage.next();
    
        for (var i = 0; i < 3; i++) {
            if ( !$next.length ) break; // This line stops your loop if there isn't any next
            var img = new Image();
            img.src = next.attr("alt");
            $next = $next.next("[alt]"); // Only takes elements with the alt attribute.
        }
    
        navigate( $nextimage, 'right' );
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a link to a question I had asked two days back, How
I had asked this question in a much more long-winded way a few days
Few months back, I had come across this interesting scenario asked by a guy
I believe that this question has been asked in a few different forms, but
I had asked this question Adding more attributes to LINQ to SQL entity Now,
This question may have been asked before, but I had trouble finding an answer,
I had the same question as was asked in this thread , i.e. I
A co-worker asked about some code like this that originally had templates in it.
I had an interesting job interview experience a while back. The question started really
This question has been asked a lot and there are quite a few answers,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.