Same-origin URLs

JavaScript, Object · Apr 22, 2021

Checks if two URLs are on the same origin.

  • Use URL.protocol and to check if both URLs have the same protocol and host.
const isSameOrigin = (origin, destination) =>
  origin.protocol === destination.protocol && ===;
const origin = new URL('');
const destination = new URL('');
isSameOrigin(origin, destination); // true
const other = new URL(';
isSameOrigin(origin, other); // false

Written by Angelos Chalaris

I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.

If you want to keep in touch, follow me on GitHub or Twitter.

More like this

  • URLs in JavaScript

    Snippet collection

    Working with URLs is an essential skill for web developers. This snippet collection covers all the necessary resources to master URLs in JavaScript.

  • Check object equality

    JavaScript, Object

    Performs a deep comparison between two values to determine if they are equivalent.

  • Deep merge objects

    JavaScript, Object

    Deeply merges two objects, using a function to handle keys present in both.

  • Query string to object

    JavaScript, Object

    Generates an object from the given query string or URL.