js中substring或split方法取得URL中的域名

1、split方式

<html>

<head></head>

<body onload="convertTemp()">

<script language="javaScript">

function convertTemp()

{

   var the_url = prompt("What's the URL?","");

var first_split = the_url.split("//");

alert(first_split[0]);

  

   var without_resource = first_split[1];

var second_split = without_resource.split("/");

var domain = second_split[0];

  

   alert(domain);

}

</script>

</body>

</html>

2、substring方式

<html>

<head></head>

<body onload="convertTemp()">

<script language="javaScript">

function convertTemp()

{

   var the_url = prompt("What's the URL?","");

var lead_slashes = the_url.indexOf("//");

var domain_start = lead_slashes + 2;

var without_resource = the_url.substring(domain_start, the_url.length);

var next_slash = without_resource.indexOf("/");

var domain = without_resource.substring(0, next_slash);

  

   alert(domain);

}

</script>

</body>

</html>

执行效果图:

js中substring或split方法取得URL中的域名

js中substring或split方法取得URL中的域名

上一篇:java命名规范和编程技巧


下一篇:飘逸的python - 用urlparse从url中抽离出想要的信息