What are third-party cookies? Why do browsers block third-party cookies?

👨‍💻 Frontend Developer 🟠 May come up 🎚️ Medium
#JavaScript #Browser #JS Basics

Brief Answer

Third-party cookies are cookies set by a domain different from the one where user is located. Browsers block them to protect user privacy from cross-site tracking. 🚫

// First-party cookie (allowed):
// Site example.com sets cookie for example.com ✅
 
// Third-party cookie (blocked):
// Site tracker.com tries to set cookie 
// when user is on example.com ❌

Full Answer

Third-party cookies are like a third-party observer who watches you on different sites through the same identifier! 👀

What are third-party cookies

First-party cookies

// When you're on site example.com
// And site example.com sets cookie
document.cookie = 'theme=dark'; // First-party cookie ✅
// This is allowed — site works with its own data

Third-party cookies

// When you're on site example.com
// But ad network tracker.com tries to set cookie
// through iframe or script on example.com
// This is third-party cookie ❌

How third-party cookies work

Tracking example

// User visits different sites:
// news.com → tracker.com sets cookie ID123
// shop.com → tracker.com sees same cookie ID123
// blog.com → tracker.com sees ID123 again
 
// Result: tracker.com knows this is one person
// on different sites! This violates privacy! 🕵️

Why browsers block them

Privacy protection

// ❌ Problem — cross-site tracking
// One tracker can collect data:
// - What you read
// - What you buy  
// - What you write
// - Where you go
 
// ✅ Solution — blocking third-party cookies
// Now tracker doesn't see you across sites

Blocking example

// In modern browsers:
// Chrome, Firefox, Safari block third-party cookies by default
 
// Before:
// tracker.com could set cookie on news.com
// Now:
// tracker.com CANNOT set cookie on news.com

When third-party cookies are useful

Social login

// ✅ Sometimes useful:
// Login via Facebook/Google on different sites
// But now use other technologies (OAuth)

Ad networks

// ✅ Used before:
// Personalized advertising
// But now there are alternatives (FLoC, Topics API)

Modern alternatives

Privacy Sandbox

// Google develops new approaches:
// - FLoC (Federated Learning of Cohorts)
// - Topics API
// - And other privacy protection technologies

Common mistakes

Thinking all cookies are blocked

// ❌ Mistake — only third-party are blocked
document.cookie = 'mysite=value'; // First-party — works!
 
// ✅ Correct — only foreign cookies are blocked
// Cookies from other domains on your site

Ignoring changes

// ❌ Bad — relying on third-party cookies
// Soon they'll be gone completely!
 
// ✅ Good — use modern approaches
// First-party data, server solutions

Simple rules

  1. First-party — cookies from same site ✅
  2. Third-party — cookies from other domains ❌
  3. Blocked — for privacy protection 🛡️
  4. Tracking — main reason for blocking 🚫
  5. Alternatives — Privacy Sandbox and other technologies 🔄

Understanding third-party cookies helps develop modern and private web applications! 💪


Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪