How do you get the URL
Window interface has access to the
Location object that can be used via
Here's what the method looks like in practice:
This method will return a USVString containing the
port number of the URL. If the URL doesn't have a
port number, it will return an empty string value (
Here are some example returned values to expect:
// Page URL: http://localhost:3000 window.location.port == "3000" // Page URL: https://coderrocketfuel.com:80/courses window.location.port == "80" // Page URL: https://coderrocketfuel.com/courses window.location.port == ""
Notice that a URL without a
port number will return an empty string value.
For more information on the browser support for this method, see its MDN web docs page.
Thanks for reading and happy coding!