← Back to Frontend, page 3

frontend-0055 · Easy fresher

What is the same-origin policy, and which differences make two URLs different origins?

An origin is the scheme, host and port of a URL. The same-origin policy lets a page freely read data only from its own origin, and it limits scripts from reading data of other origins. A different scheme, host or port makes a different origin, and subdomains are different origins too.

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/courses and https://schoolabe.com/about are the same origin. The path does not matter.
  • http://schoolabe.com and https://schoolabe.com differ in scheme.
  • https://schoolabe.com and https://www.schoolabe.com differ in host. A subdomain is a different origin.
  • https://schoolabe.com and https://schoolabe.com:8443 differ 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 fetch or XMLHttpRequest, unless the other server allows it with CORS.
  • Scripts cannot read the DOM of an iframe from another origin.
  • localStorage, IndexedDB and 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.com and www.schoolabe.com are the same site and different origins. Cookie rules (SameSite) use sites.
  • document.domain relaxation is deprecated and should not be used.
  • Where a page needs to talk to another origin on purpose, use CORS for data, and postMessage for windows.

The script compares several URLs with the standard URL class, which exposes the origin of each.

javascript
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.