Computer >> Computer tutorials >  >> Programming >> Javascript

How to extract the hostname portion of a URL in JavaScript?


To extract the hostname portion of a URL, use the hostname method

Example

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <script>
         var url = new URL("https://example.com/asdf/asdf/sadf.aspx?blah");
         document.write(url.hostname);
      </script>
   </body>
</html>