How to check the URL of a page using JavaScript

How would I make a script that detects the current URL of the page? Here is what I want it to do:

When the script is on example.com it lets the page load normally.

When the script is on coolsite.com it replaces the content of the page with a note showing a link to example.com

How would I do that? Is there a function that detects the location of the window? I can put it in a <script> element or a separate JavaScript file.

Thank you for any help!