Get Current URL in JavaScript

Sometimes, we need to get the current page URL that is shown in the browser URL window so today we explain to you how to get Current URL in web browser using JavaScript.

Get Current URL in JavaScript, Get Current URL in Web Browser using JavaScript, get the current browser URL using JavaScript, JavaScript Window Location, Get full web page URL from the address bar, location property.

URL is composed of many different sub parts and the location object contains information about the current URL.

If current url is https://www.cluemediator.com:80/examples?q='pqr'#abc and we hit the window.location command in console window then we will get below output.

Now we will explain the properties of location object. So assume that the current URL is https://www.cluemediator.com:80/examples?q='pqr'#abc.

Property of location object

  1. href
  2. protocol
  3. host
  4. hostname
  5. port
  6. pathname
  7. search
  8. hash

1. href

window.location.href will return the entire url of the current document URL.

2. protocol

window.location.protocol will return the protocol of the current document URL.

3. host

window.location.host will return the host part and port number of the current document URL.

4. hostname

window.location.hostname will return only the hostname of the current document URL.

5. port

window.location.port will return only the port number of the current document URL.

6. pathname

window.location.pathname will return the pathname of the current document URL.

window.location.search will return the queryString part of the current document URL.

8. hash

window.location.hash will return the anchor part of the current document URL.

Hope this will help you!

Thanks for reading and happy coding!

You may also like...

2 Responses

  1. Rachele says:

    I’m impressed, I must say. Rarely do I encounter a
    blog that’sboth educative and entertaining, and lett me tell you,
    you’ve hit the nail on the head. The problem is something which too few folks
    are speaking intelligently about. I am very hhappy that I
    stumbled across this in my search for something regarding this.

    • Clue Mediator says:

      I’m glad you like the article. We are sharing the knowledge everyday so please share it with your friends.

      Like & Share it. Happy Coding..!!!

Leave a Reply

Your email address will not be published. Required fields are marked *