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 7716193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:34:33+00:00 2026-06-01T02:34:33+00:00

I am new to JavaScript. I created the webpage linked below as an exercise

  • 0

I am new to JavaScript. I created the webpage linked below as an exercise for a class I am taking.

JavaScript Product Catalog

It seems to work ok if it all loads correctly, but half the time either the images or buttons do not load, and sometimes the thumbnail loads but the full-size (mouseover) image does not load. In Firefox or IE, it seems to only happen on first loads, and after that (as long as browser stays open), it will successfully load every time after that. But in Chrome it continues to act random every single page reload. Just refresh the page about 10 times in a row and you will probably see a couple instances where either the buttons or the images (or both) don’t load.

I am assuming this is a problem with my code, since I’ve never had any other problems with the server. Any ideas?

Thanks!

<!DOCTYPE html>

<html>
<head>
   <meta charset = "utf-8">
   <style type = "text/css">
      .box { border: 1px solid black; padding: 4px }
   </style>
   <title>Product Catalog</title>
   <script>

   var catalogDiv;
   var summaryRequest;
   var descriptionsRequest;
   var thumbsRequest;
   var imagesRequest;

   function showLargeImage( imageElement )
   {
       imageElement.style.display = "none";
       imageElement.nextSibling.style.display = "inline";
   }

   function showThumb( imageElement )
   {
       imageElement.style.display = "none";
       imageElement.previousSibling.style.display = "inline";
   }

   function showDesc( descButton )
   {
      if ( descButton.nextSibling.style.display == "none" ) {
         descButton.nextSibling.style.display = "block";
      } else {
         descButton.nextSibling.style.display = "none";
      }
   }

   function getDescriptions()
   {
      try
      {
         descriptionsRequest = new XMLHttpRequest();
         descriptionsRequest.addEventListener("readystatechange",
            loadDescriptions, false );
         descriptionsRequest.open( "GET", "descriptions.json", true );
         descriptionsRequest.setRequestHeader( "Accept",
            "application/json; charset=utf-8" );
         descriptionsRequest.send();
      }
      catch ( exception )
      {
         alert( "Request Failed" );
      }
   }

   function loadDescriptions()
   {
      if ( descriptionsRequest.readyState == 4
         && descriptionsRequest.status == 200 )
      {
         var descriptions = JSON.parse( descriptionsRequest.responseText );
         for ( var i = 0; i < descriptions.length; i++ ) {
            var infoDiv = document.getElementById( descriptions[i].id +
               "-info-inner" );

            var descButton = document.createElement( "button" );
            infoDiv.appendChild( descButton );
            descButton.type = "button";
            descButton.textContent = "show description";
            descButton.setAttribute( "onclick", "showDesc( this )");

            var desc = document.createElement( "fieldset" );
            desc.style.display = "none";
            desc.style.margin = "10px";
            infoDiv.appendChild( desc );
            desc.innerHTML = "<br>" + descriptions[i].text + "<br><br>" ;
         }
      }
   }

   function getImages()
   {
      try
      {
         imagesRequest = new XMLHttpRequest();
         imagesRequest.addEventListener("readystatechange",
            loadImages, false );
         imagesRequest.open( "GET", "images.json", true );
         imagesRequest.setRequestHeader( "Accept",
            "application/json; charset=utf-8" );
         imagesRequest.send();
      }
      catch ( exception )
      {
         alert( "Request Failed" );
      }
   }

   function loadImages()
   {
      if ( imagesRequest.readyState == 4 && imagesRequest.status == 200 )
      {
         var images = JSON.parse( imagesRequest.responseText );
         for ( var i = 0; i < images.length; i++ ) {
            var imageDiv = document.getElementById( images[i].id +
               "-image-inner" );
            imageDiv.innerHTML += "<img style=\"display:none;\"" +
               "src=\"" + images[i].filename+ "\">";
            imageDiv.lastChild.setAttribute( "onmouseout",
               "showThumb( this )" );
         }
      }
   }   

   function getThumbs()
   {
      try
      {
         thumbsRequest = new XMLHttpRequest();
         thumbsRequest.addEventListener("readystatechange",
            loadThumbs, false );
         thumbsRequest.open( "GET", "thumbs.json", true );
         thumbsRequest.setRequestHeader( "Accept",
            "application/json; charset=utf-8" );
         thumbsRequest.send();
      }
      catch ( exception )
      {
         alert( "Request Failed" );
      }
   }

   function loadThumbs()
   {
      if ( thumbsRequest.readyState == 4 && thumbsRequest.status == 200 )
      {
         var thumbs = JSON.parse( thumbsRequest.responseText );
         for ( var i = 0; i < thumbs.length; i++ ) {
            var imageDiv = document.getElementById( thumbs[i].id +
               "-image-inner" );
            imageDiv.innerHTML = "<img style=\"display:inline;\"" +
               "src=\"" + thumbs[i].filename+ "\">";
            imageDiv.firstChild.setAttribute( "onmouseover",
               "showLargeImage( this )");
         }
      }
   }   

   function setupDivsRequest()
   {
      try
      {
         summaryRequest = new XMLHttpRequest();
         summaryRequest.addEventListener("readystatechange",
            setupDivsResponse, false );
         summaryRequest.open( "GET", "summary.json", true );
         summaryRequest.setRequestHeader( "Accept",
            "application/json; charset=utf-8" );
         summaryRequest.send();
      }
      catch ( exception )
      {
         alert( "Request Failed" );
      }
   }

   function setupDivsResponse()
   {
      if ( summaryRequest.readyState == 4 && summaryRequest.status == 200 )
      {
         var summary = JSON.parse( summaryRequest.responseText );

         for ( var i = 0; i < summary.length; i++ ) {

            var productDiv = document.createElement( "div" );
            var productImageOuterDiv = document.createElement( "div" );
            var productImageInnerDiv = document.createElement( "div" );
            var productInfoOuterDiv = document.createElement( "div" );
            var productInfoInnerDiv = document.createElement( "div" );

            catalogDiv.appendChild(productDiv);
            productDiv.appendChild( productImageOuterDiv );
            productDiv.appendChild( productInfoOuterDiv );
            productImageOuterDiv.appendChild( productImageInnerDiv );
            productInfoOuterDiv.appendChild( productInfoInnerDiv );

            productDiv.id = summary[i].id;
            productDiv.className = "box";

            productImageOuterDiv.id = summary[i].id + "-image-outer";
            productImageOuterDiv.style.cssFloat = "left";

            productImageInnerDiv.id = summary[i].id + "-image-inner";
            productImageInnerDiv.style.height = "250px";
            productImageInnerDiv.style.width = "250px";
            productImageInnerDiv.style.display = "table-cell";
            productImageInnerDiv.style.verticalAlign = "middle";
            productImageInnerDiv.style.textAlign = "center";

            productInfoOuterDiv.id = summary[i].id + "-info-outer";
            productInfoOuterDiv.style.height = "250px";

            productInfoInnerDiv.id = summary[i].id + "-info-inner";
            productInfoInnerDiv.style.float = "left";
            productInfoInnerDiv.style.padding = "10px";

            productInfoInnerDiv.innerHTML = summary[i].title + "<br>";
            productInfoInnerDiv.innerHTML += summary[i].price + "<br><br>";
         }
      }
   }   

   function start()
   {
      catalogDiv = document.getElementById( "catalog" );
      setupDivsRequest();
      getThumbs();
      getImages();
      getDescriptions();
   }

   window.addEventListener( "load", start, false );
   </script>
</head>
<body>
   <h1>Mouse over a product thumbnail for a larger picture.</h1>
   <div id = "catalog"></div>
</body>
</html>
  • 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-06-01T02:34:34+00:00Added an answer on June 1, 2026 at 2:34 am

    It took a long time, but I finally got to the bottom of this problem. It was a race condition between multiple asynchronous requests to populate the same element. I had not considered this was possible, so the one which I expected to go first would add the first HTML to the element:

    element.innerHTML = "first text";
    

    While the request which I expected to go second would add the second HTML:

    element.innerHTML += "second text";
    

    Obviously if those requests go out of order, because of the way I used = and +=, the result will be that “second text” gets overwritten, which is essentially why my images weren’t loading half the time. (Even if I had used += in both cases, I’d still have the problem of randomly ordered elements as my code below shows).

    For whatever reason, the race condition never seemed to matter in Firefox or IE. Maybe there is something in those browsers to try to safeguard against such a condition, by forcing requests to finish in the order they started? Or maybe it is just dumb luck. But in Chrome, the requests would consistently finish in a random order. A much simpler code below illustrates clearly. In Chrome, half the time you will get “FOOBAR” as HTML output, but the other half of the time you will get “BARFOO.” The testx.json files I reference in the script are dummy (empty) files.

    The race condition is easily fixed in this situation by having my second setup function called by the first setup’s callback function after completing its other tasks. In a more complicated situation I would guess the other typical race condition safeguards (mutexes and semaphores) would work as well.

    <!DOCTYPE html>
    
    <html>
    <head>
       <script>
    
       var testDiv;
       var request1;
       var request2;
    
       window.addEventListener( "load", start, false );
    
       function start()
       {
          testDiv = document.getElementById( "test-div" );
          setup1();
          setup2();
       }
    
       function setup1()
       {
          try
          {
             request1 = new XMLHttpRequest();
             request1.addEventListener("readystatechange",
                response1, false );
             request1.open( "GET", "test1.json", true );
             request1.setRequestHeader( "Accept",
                "application/json; charset=utf-8" );
             request1.send();
          }
          catch ( exception )
          {
             alert( "Request Failed" );
          }
       }
    
       function response1()
       {
          if ( request1.readyState == 4 && request1.status == 200 )
          {
             testDiv.innerHTML += "FOO";
          }
       } 
    
       function setup2()
       {
          try
          {
             request2 = new XMLHttpRequest();
             request2.addEventListener("readystatechange",
                response2, false );
             request2.open( "GET", "test2.json", true );
             request2.setRequestHeader( "Accept",
                "application/json; charset=utf-8" );
             request2.send();
          }
          catch ( exception )
          {
             alert( "Request Failed" );
          }
       }
    
       function response2()
       {
          if ( request2.readyState == 4 && request2.status == 200 )
          {
             testDiv.innerHTML += "BAR";
          }
       }   
    
       </script>
    </head>
    <body>
       <div id = "test-div"> </div>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Nodejs and Socket.io. When the client connects, new JavaScript objects are created.
im new at javascript and i can get this slider to work but not
I'm new to javascript and I'm trying to learn. I'd like my webpage to
So my first Web and JavaScript project with EClipse: I created a File->New General
I'm new to Javascript/Jquery and PHP and I'm experimenting with it. Basically, I've created
I'm new to javascript and starting the mix javascript + jquery + coffeescript all
I created a simple Google Maps webpage using Javascript. I parse an XML file
I'm still new to Javascript and would like to create a 'pages' feature for
I have some code that dynamically creates a new button through JavaScript that when
I am trying to create a new Element in my javascript code and append

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.