I had a magical idea:

โ€œWhat if I could build a toy store that sends me a catalog โ€” without stepping outside?โ€

No capes. No traffic. Just code. ๐Ÿง™๐Ÿฝโ€โ™‚๏ธโœจ And thatโ€™s how I created a simple webpage that uses JavaScriptโ€™s superpowers to fetch toys from a pretend catalog file โ€” all by clicking a button.

Let me tell you how I did it, and how you can do it too! ๐Ÿš€


๐Ÿ›๏ธ The Idea

I imagined a toy store that has a list of toys stored in a file called toys.txt. The user clicks a button, and JavaScript goes to fetch that file, then shows the toys on the screen. Itโ€™s like:

  • Sending a messenger bird (using XMLHttpRequest)
  • Or a delivery drone (using fetch())

Either way, the toys arrive like magic on the page.


๐Ÿ“ Project Setup

I created a folder called toy-store and placed these two files inside:

toy-store/
โ”œโ”€โ”€ index.html     โ† the web page
โ””โ”€โ”€ toys.txt       โ† the toy catalog

๐Ÿงธ toys.txt

Hereโ€™s what I put inside the toys.txt file:

๐Ÿงธ Teddy Bear
๐Ÿš— Race Car
๐Ÿงฉ Puzzle Set
๐ŸŽฎ Game Console
๐Ÿช Flying Kite

๐ŸŒ Creating the Web Page: index.html

Now for the fun part โ€” the web page!

Hereโ€™s the full code I wrote in index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Toy Store Catalog</title>
  <style>
    body {
      font-family: 'Comic Sans MS', cursive;
      background-color: #f9faff;
      text-align: center;
      padding: 2rem;
    }

    button {
      padding: 0.6rem 1.2rem;
      font-size: 1rem;
      background-color: #0099cc;
      color: white;
      border: none;
      border-radius: 8px;
      margin: 1rem;
      cursor: pointer;
    }

    #output {
      margin-top: 2rem;
      padding: 1rem;
      border: 2px dashed #0099cc;
      background-color: #e8f9ff;
      white-space: pre-line;
    }
  </style>
</head>
<body>

  <h1>๐Ÿงธ Welcome to the JavaScript Toy Store</h1>

  <button onclick="useMessengerBird()">Use Messenger Bird ๐Ÿ•Š๏ธ</button>
  <button onclick="useSpeedyDrone()">Use Speedy Drone ๐Ÿš</button>

  <div id="output">๐Ÿ“ฆ Waiting for the toy catalog...</div>

  <script>
    // Method 1: The Old-School Messenger Bird (XMLHttpRequest)
    function useMessengerBird() {
      const xhttp = new XMLHttpRequest();
      xhttp.onload = function () {
        if (this.status === 200) {
          document.getElementById('output').innerText = this.responseText;
        } else {
          document.getElementById('output').innerText = "โŒ Toy catalog not found!";
        }
      };
      xhttp.open("GET", "toys.txt", true);
      xhttp.send();
    }

    // Method 2: The Modern Speedy Drone (fetch API)
    function useSpeedyDrone() {
      fetch("toys.txt")
        .then(response => {
          if (!response.ok) {
            throw new Error("Network issue: " + response.status);
          }
          return response.text();
        })
        .then(data => {
          document.getElementById("output").innerText = data;
        })
        .catch(error => {
          document.getElementById("output").innerText = "โŒ Error: " + error.message;
        });
    }
  </script>

</body>
</html>

๐Ÿง  How It Works

When you click a button:

  • Messenger Bird (XMLHttpRequest) flies to get the toy catalog file and brings it back.
  • Speedy Drone (fetch) does the same thing but using promises, which makes the code cleaner and modern.

The toy list is then displayed in the #output box like a gift opened on your birthday. ๐ŸŽ


๐Ÿงช Try It Yourself

To run this project smoothly:

  1. Open the folder in VS Code.
  2. Install the Live Server extension.
  3. Right-click index.html and click โ€œOpen with Live Serverโ€.
  4. Click the buttons and watch the toys appear!

Note: If you open the file without a server, the browser might block file fetching for security reasons.


๐Ÿงฉ What I Learned

  • XMLHttpRequest is like a hardworking messenger from the past.
  • fetch() is the new trend โ€” it uses promises to simplify things.
  • Both can send GET requests to grab files and display them on a webpage.

๐Ÿ“š Review Questions

  1. What file format did we use to store the toy catalog?
  2. Whatโ€™s the difference between XMLHttpRequest and fetch()?
  3. Why do we need a local server to run this project properly?
  4. What happens when the request fails (e.g., file not found)?
  5. How would you make this toy store more interactive?

<
Previous Post
๐Ÿ›ธ Python Loop: The Loopy Tale of Zina the Python Apprentice
>
Next Post
๐Ÿ“š JavaScript Adventure: const vs Object.freeze() โ€” The Tale of the Unchangeable Toy List