On this page

A small working browser clicker

The example below creates a separate learning project, not a copy of the official Cookie Clicker source. Save it as index.html in a new folder and open that file in a browser. It needs no library, account or network request.

Click the cookie until you have 15 cookies, then buy a baker. Each baker adds one cookie per second while the page is running. The next baker costs more, and the button becomes disabled when you cannot afford it.

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>My practice bakery</title>
<h1>My practice bakery</h1>
<p id="score">0 cookies</p>
<button id="cookie" type="button">Bake a cookie</button>
<button id="buy" type="button">Buy baker: 15</button>
<script>
let cookies = 0, bakers = 0, price = 15;
const score = document.querySelector('#score');
const buy = document.querySelector('#buy');
function render() {
  score.textContent = cookies + ' cookies; ' + bakers + ' per second';
  buy.textContent = 'Buy baker: ' + price;
  buy.disabled = cookies < price;
}
document.querySelector('#cookie').addEventListener('click', () => {
  cookies += 1;
  render();
});
buy.addEventListener('click', () => {
  if (cookies < price) return;
  cookies -= price;
  bakers += 1;
  price = Math.ceil(price * 1.15);
  render();
});
setInterval(() => { cookies += bakers; render(); }, 1000);
render();
</script>
</html>

How the HTML and JavaScript work

The HTML provides a score and two buttons. JavaScript keeps three numbers: the balance, owned bakers and next price. render updates the interface from those numbers. The shop checks affordability before subtracting the price, then increases ownership and the next price.

One interval supplies passive income. Keeping that timer outside the click handlers avoids accidentally starting a second production loop with each purchase. The event handlers update textContent rather than inserting HTML from a player’s input.

Test it before adding features

After 14 clicks the baker button should still be disabled. At 15 clicks, buy one baker: the balance becomes zero, the next price becomes 18, and one cookie arrives each second. Refreshing resets this example; persistent saving, offline production and upgrades are not implemented.

Browser timers can be throttled in background tabs. A larger game should measure elapsed time and validate saved data rather than assuming each timer callback represents an exact second. For a blocks-based alternative, use the Scratch clicker tutorial. For playing the official game, browse all guides.