Get the current URL with JavaScript?

后端 未结 23 1974
梦谈多话
梦谈多话 2020-11-21 07:08

All I want is to get the website URL. Not the URL as taken from a link. On the page loading I need to be able to grab the full, current URL of the website and set it as a va

23条回答
  •  伪装坚强ぢ
    2020-11-21 07:57

    Adding result for quick reference

    window.location;

     Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript",
     ancestorOrigins: DOMStringList,
     origin: "https://stackoverflow.com",
     replace: ƒ, assign: ƒ, …}
    

    document.location

      Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript", 
    ancestorOrigins: DOMStringList,
     origin: "https://stackoverflow.com",
     replace: ƒ, assign: ƒ
    , …}
    

    window.location.pathname

    "/questions/1034621/get-the-current-url-with-javascript"
    

    window.location.href

    "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript"
    

    location.hostname

    "stackoverflow.com"
    

提交回复
热议问题