How to get base domain from the URL in JavaScript

后端 未结 9 562
萌比男神i
萌比男神i 2020-12-06 02:52

I would like to extract the base domain from the url in javascript. For example for the list of urls listed below I need to get google.com (or googl

9条回答
  •  余生分开走
    2020-12-06 03:25

    How to get base domain from the URL in JavaScript

    document.location.origin

    Result: "https://stackoverflow.com"

    And sometimes you might also want these:

    document.location.hostname

    Result: "stackoverflow.com"

    document.location.pathname

    Result: "/questions/6449340/how-to-get-base-domain-from-the-url-in-javascript/58887093"

提交回复
热议问题