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

JavascriptWeb DevelopmentFront End Technology

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

Example

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <script>
         var url = new URL("http://example.com/asdf/asdf/sadf.aspx?blah");
         document.write(url.hostname);
      </script>
   </body>
</html>
raja
Published on 29-Jan-2018 11:21:16
Advertisements