From 2e61419df8a1c86a54c42e0859e281ea7027d4c5 Mon Sep 17 00:00:00 2001 From: Chalarangelo Date: Thu, 22 Apr 2021 08:27:41 +0300 Subject: [PATCH] Add isSameOrigin --- snippets/isSameOrigin.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 snippets/isSameOrigin.md diff --git a/snippets/isSameOrigin.md b/snippets/isSameOrigin.md new file mode 100644 index 000000000..273861b37 --- /dev/null +++ b/snippets/isSameOrigin.md @@ -0,0 +1,21 @@ +--- +title: isSameOrigin +tags: object,beginner +--- + +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. + +```js +const isSameOrigin = (origin, destination) => + origin.protocol === destination.protocol && origin.host === destination.host; +``` + +```js +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 +```