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>
执行效果图:
最新文章
- SpringMVC访问静态资源的三种方式(转)
- ofbiz进击 个人遇到的奇葩问题汇总。
- 淮安团购网美团联盟网赚版 v5.7
- mysql中bigint在php中表示
- chrome调试ajax
- USACO 3.3 A Game
- CSS用足够大的纯色内阴影去覆盖掉谷歌input记住账号或密码时默认出现的黄色背景
- (java web后端方向)如何让你的简历为你争取到更多的面试机会,内容来自java web轻量级开发面试教程
- 转:Flutter动画二
- Devexpress GridControl 多选
- java io系列12之 BufferedInputStream(缓冲输入流)的认知、源码和示例
- TCP/IP笔记(1)
- 函数和常用模块【day05】:迭代器(六)
- Angular4 Ng 模块
- 帝国cms建站总结-(分页)
- 使用pymongo连接mongodb时报错:pymongo.errors.OperationFailure: not authorized
- ZooKeeper参数调优
- 【转】SQL SERVER 中 sp_rename 用法
- Eclipse 调试总进入Spring代理的解决办法
- 551. Student Attendance Record I + Student Attendance Record II