The origin of a URL indicates where a document, HTTP request, or web page originated from. It doesn't include any path information, but only the server name.
Window interface has access to the
Location object that can be used via
Using that interface, you can get the URL origin with this method:
This method will return a USVString containing Unicode serialization of the origin of the current page's URL.
Here are some example returned values to expect:
// Page URL: https://coderrocketfuel.com window.location.origin == "https://coderrocketfuel.com" // Page URL: https://coderrocketfuel.com/courses window.location.origin == "https://coderrocketfuel.com" // Page URL: https://subdomain.coderrocketfuel.com window.location.origin == "https://subdomain.coderrocketfuel.com"
To quickly test this, open the developer tools in your favorite browser and execute the
For more information on the browser support for this method, see its MDN web docs page.
Thanks for reading and happy coding!