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>
执行效果图: