Files
30-seconds-of-code/snippets/is-same-origin.md
Angelos Chalaris 61200d90c4 Kebab file names
2023-04-27 21:58:35 +03:00

720 B

title, tags, author, cover, firstSeen, lastUpdated
title tags author cover firstSeen lastUpdated
Same-origin URLs browser chalarangelo keyboard-tea 2021-04-22T08:27:41+03:00 2021-04-22T08:27:41+03:00

Checks if two URLs are on the same origin.

  • Use URL.protocol and URL.host to check if both URLs have the same protocol and host.
const isSameOrigin = (origin, destination) =>
  origin.protocol === destination.protocol && origin.host === destination.host;
const origin = new URL('https://www.30secondsofcode.org/about');
const destination = new URL('https://www.30secondsofcode.org/contact');
isSameOrigin(origin, destination); // true
const other = new URL('https://developer.mozilla.org);
isSameOrigin(origin, other); // false