Full answer
The same-origin policy is a core browser security rule. Without it, any page you open could use your logged-in session to read your email in another tab.
An origin has three parts: scheme, host and port. Two URLs are the same origin only if all three match.
https://schoolabe.com/coursesandhttps://schoolabe.com/aboutare the same origin. The path does not matter.http://schoolabe.comandhttps://schoolabe.comdiffer in scheme.https://schoolabe.comandhttps://www.schoolabe.comdiffer in host. A subdomain is a different origin.https://schoolabe.comandhttps://schoolabe.com:8443differ in port. The default port of a scheme (443 for https, 80 for http) is the same as writing no port.
What the policy limits:
- Scripts cannot read responses from other origins with
fetchorXMLHttpRequest, unless the other server allows it with CORS. - Scripts cannot read the DOM of an iframe from another origin.
localStorage,IndexedDBand cookies are separated by origin or by site, depending on the storage type.
What it does not limit: a page can still embed images, scripts, stylesheets and frames from other origins, and it can send cross-origin form posts and simple requests. It limits reading. That is why CSRF exists and why third-party scripts are so powerful: a script loaded from another host runs with the origin of the page that loads it.
Related words:
- Site is a larger idea: the scheme plus the registrable domain, such as
schoolabe.com.app.schoolabe.comandwww.schoolabe.comare the same site and different origins. Cookie rules (SameSite) use sites. document.domainrelaxation is deprecated and should not be used.- Where a page needs to talk to another origin on purpose, use CORS for data, and
postMessagefor windows.
The script compares several URLs with the standard URL class, which exposes the origin of each.
const base = 'https://schoolabe.com/courses';
const cases = [
['https://schoolabe.com/about', 'other path'],
['http://schoolabe.com/courses', 'other scheme'],
['https://www.schoolabe.com/courses', 'subdomain'],
['https://schoolabe.com:8443/courses', 'other port'],
['https://schoolabe.com:443/courses', 'default port written out'],
];
for (const [url, label] of cases) {
console.log(`${new URL(url).origin === new URL(base).origin} - ${label}`);
}Run with Node 24.22.1. The output matched the text in the answer in two runs.
Key points
- An origin is scheme, host and port. The path is not part of it.
- A subdomain or a different port is a different origin.
- The policy limits reading cross-origin responses and frames, not embedding.
- A site is broader than an origin, and cookies use sites.
- CORS and postMessage are the controlled exceptions.
Common mistakes
- Thinking the path is part of the origin.
- Saying subdomains share an origin.
- Believing the policy stops a page from sending cross-origin requests at all.
- Mixing up origin and site.
Follow-ups
- Why can a page include a script from a CDN?
- How do origin and site differ for cookies?
- What does a script from another origin run as?
Tips
- Say the three parts, then give a table of examples.
- End with what the policy does not stop.