Same-Origin Policy Fundamentals
You'll learn the core browser security rule that controls when web pages can read or manipulate each other's data.
What Is the Same-Origin Policy?
The Same-Origin Policy (SOP) is the browser's fundamental security boundary. It prevents scripts running in one web page from accessing data in another web page unless both pages come from the exact same "origin."
Think of it like apartment buildings: tenants in Apartment 3B can't walk into Apartment 4A and rummage through their belongings, even though they're in the same building. Similarly, a script from https://bank.com cannot read the content of a page from https://evil.com, even if both are open in your browser.
What Defines an Origin?
An origin is the combination of three components:
- Protocol (scheme):
httpvshttps - Domain (host):
example.comvsapi.example.com - Port:
:80vs:443vs:8080
All three must match exactly for two pages to be "same-origin."
Examples
https://example.com/page1andhttps://example.com/page2→ Same origin ✓https://example.comandhttp://example.com→ Different origins (protocol differs) ✗https://example.comandhttps://api.example.com→ Different origins (subdomain differs) ✗https://example.com:443andhttps://example.com:8080→ Different origins (port differs) ✗
Why It Matters
Without SOP, any website you visit could read your emails from Gmail, drain your bank account, or steal data from other open tabs. The Same-Origin Policy is what makes it safe to have multiple websites open simultaneously—they're isolated from each other by default.
Key Takeaway: The Same-Origin Policy restricts cross-origin document interaction based on matching protocol, domain, and port—forming the browser's most critical security boundary.