Files
30-seconds-of-code/snippets/isSameOrigin.md
Angelos Chalaris 8a6b73bd0c Update covers
2023-02-16 22:24:28 +02:00

26 lines
719 B
Markdown

---
title: Same-origin URLs
tags: object
author: chalarangelo
cover: keyboard-tea
firstSeen: 2021-04-22T08:27:41+03:00
lastUpdated: 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.
```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
```