Skip to main content

How to get url of current page in javascript? window.location in js.

How to get current Page URL using JavaScript - YouTube

This is very basic and hot question asking every time in interview. How to get current page url in js? 

syntax :- window.location
  1. Location {href: "https://boyonweb.blogspot.com/2020/05/how-to-use-every-function-in-javascript.html", ancestorOrigins: DOMStringList, origin: "https://boyonweb.blogspot.com", protocol: "https:", host: "boyonweb.blogspot.com", …}
    1. hash""
    2. host"boyonweb.blogspot.com"
    3. hostname"boyonweb.blogspot.com"
    4. href"https://boyonweb.blogspot.com/2020/05/how-to-use-every-function-in-javascript.html"
    5. origin"https://boyonweb.blogspot.com"
    6. pathname"/2020/05/how-to-use-every-function-in-javascript.html"
    7. port""
    8. protocol"https:"
    9. search""
    10. Symbol(Symbol.toPrimitive)undefined

Here you will get one JSON object with multiple keys. lets understand few important keys:-


  • host"boyonweb.blogspot.com" - host will return you domain with extension without http/https.
  • href"https://boyonweb.blogspot.com/2020/05/how-to-use-every-function-in-javascript.html" - this
  • key will give complete full url of current page.
  • origin"https://boyonweb.blogspot.com"- origin key will give host name with its protocol.
  • pathname"/2020/05/how-to-use-every-function-in-javascript.html" - it will give path with protocol
  • and domain name
  • protocol"https:"- it will give you protocal type like https/http/ftp etc.


Example of One website you can see here :



window.location
  1. Location {href: "https://marketingdukaan.com/freewebsite", ancestorOrigins: DOMStringList, origin: "https://marketingdukaan.com", protocol: "https:", host: "marketingdukaan.com", …}
    1. hash""
    2. host"marketingdukaan.com"
    3. hostname"marketingdukaan.com"
    4. href"https://marketingdukaan.com/freewebsite"
    5. origin"https://marketingdukaan.com"
    6. pathname"/freewebsite"
    7. port""
    8. protocol"https:"
    9. search""
    10. Symbol(Symbol.toPrimitive)undefined

Comments

Popular posts from this blog

How to retrieve data from database and Display in JSP Page?

How to retrieve data from database and Display in JSP Page? Watch here step by step:-

How to use every function in javascript? Best 5 example of every function in js.

JavaScript every Function -  Every Function :

How to read or import excel file in angular 2,4,5,7,8,10 Hindi/English