Get The Current Domain Name With Javascript (Not the path, etc.)

前端 未结 17 2255
情歌与酒
情歌与酒 2020-12-04 05:43

I plan on buying two domain names for the same site. Depending on which domain is used I plan on providing slightly different data on the page. Is there a way for me to de

17条回答
  •  误落风尘
    2020-12-04 06:01

    Combining a few answers from the above, the following works really well for me for destroying Cookies:

      /**
       * Utility method to obtain the domain URI:
       */
      fetchDomainURI() {
        if (window.location.port.length > 0) {
          return window.location.hostname;
        }
        return `.${window.location.hostname.match(/\w*\.\w*$/gi)[0]}`;
      }
    

    Works for IP addresses with ports, e.g., 0.0.0.0:8000 etc, as well as complex domains like app.staging.example.com returning .example.com => allows for cross-domain Cookie setting and destroying.

提交回复
热议问题