如何在JavaScript中提取URL的主机名部分?

要提取URL的主机名部分,请使用主机名方法

示例

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