windowobject has a property location which contains information about the current document or the window in which the script is running.
This object can be used for getting the URL and host of the current page as below.
window.location.href : Will return the complete URL of the current page.
window.location.host: Will return the host and port from the URL of the current page.
window.location.hostname: Will return only the name of host from the URL of the current page.
window.location.port: Will return only the port from the current URL.
window.location.protocol: Will return only the protocol(http or https) over which the current page was loaded.
||127.0.0.1:8080||Host(or ip) and port from URL|
||127.0.0.1||Only host(or ip) from URL|
||8080||Only port from URL|
||http:||Only protocol of URL|
You can also use
location directly without prefixing it with
Also, note that the type of the value returned by all the above properties is a string.