/*
* @function: 通过a标签解析url标签
* @param:url url参数是字符串,解析的目标
通过IE6-9 chrome Firefox测试
*
*/
function parseURL(url) {
//创建一个a标签
var a = document.createElement('a');
//将url赋值给标签的href属性。
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':',''), //协议
host: a.hostname, //主机名称
port: a.port, //端口
query: a.search, //查询字符串
params: (function(){ //查询参数
var ret = {},
seg = a.search.replace(/^\?/,'').split('&'),
len = seg.length, i = 0, s;
for (;i<len;i++) {
if (!seg[i]) { continue; }
s = seg[i].split('=');
ret[s[0]] = s[1];
}
return ret;
})(),
file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1], //文件名
hash: a.hash.replace('#',''), //哈希参数
path: a.pathname.replace(/^([^\/])/,'/$1'), //路径
relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1], //相对路径
segments: a.pathname.replace(/^\//,'').split('/') //路径片段
};
}

  

测试地址
console.log(parseURL("http://www.w3school.com.cn/jsref/dom_obj_anchor.asp?type=2#id2"));
结果如下

{
file: "dom_obj_anchor.asp",
hash: "id2",
host: "www.w3school.com.cn",
params: {type: "2"},
path: "/jsref/dom_obj_anchor.asp",
port: "80",
protocol: "http",
query: "?type=2",
relative: "/jsref/dom_obj_anchor.asp?type=2#id2",
segments: [0: "jsref",1: "dom_obj_anchor.asp"],
source: http://www.w3school.com.cn/jsref/dom_obj_anchor.asp?type=2#id2
}

  

Javascript 利用a标签自动解析URL分析网址实例的更多相关文章

  1. 【javascript】利用 a 标签自动解析 url

    很多时候,我们有从 url 中提取域名,查询关键字,变量参数值等的需求,然而我们可以让浏览器方便地帮助我们完成这一任务而不用写正则去抓取.方法就是先创建一个 a 标签然后将需要解析的 url 赋值给  ...

  2. 利用 a 标签自动解析 url

    很多时候,我们有从 url 中提取域名,查询关键字,变量参数值等的需求,然而我们可以让浏览器方便地帮助我们完成这一任务而不用写正则去抓取.方法就是先创建一个 a 标签然后将需要解析的 url 赋值给  ...

  3. 利用a标签自动解析URL

    parseURL // This function creates a new anchor element and uses location // properties (inherent) to ...

  4. 小技巧之a标签自动解析URL

    我们可能都知道javascript中的window.location对象用来获取当前页面的地址URL,并把浏览器重定向到新的页面.它有protocol.hostname.host.port.searc ...

  5. 利用strut2标签自动生成form前端验证代码

    利用strut2标签自动生成form前端验证代码,使用到的技术有1.struts2标签,如<s:form> <s:textfieled>2.struts2读取*Validati ...

  6. 利用a标签特性解析地址(附加属性说明)

    首先我们看看实例 a.href = 'http://www.cnblogs.com/wayou/p/'; console.log(a.host); 控制台会输出 "www.cnblogs.c ...

  7. 自动解析URL

    function parseURL(url) { var a = document.createElement('a'); a.href = url; return { source: url, pr ...

  8. javascript parseUrl函数解析url获取网址url参数

    function parseURL(url) { var a = document.createElement('a'); a.href = url; return { source: url, pr ...

  9. JavaScript实现http地址自动检测并添加URL链接

    一.天生我材必有用 给http字符自动添加URL链接是比较常见的一项功能.举两个我最近常用到的自动检测http://地址并添加链接的例子吧,首先是QQ邮箱,在使用QQ邮箱时,如果输入了URL地址(ht ...

随机推荐

  1. android中一个解决办法

    E  Trace(732): error opening trace file: No such file or directory (2)android api 的版本和模拟器的版本不一致导致的

  2. js 倒计时点击和当前时间

    <input id="btn" type="submit" value="确定" disabled="disabled&qu ...

  3. yum -y list java* 查看当前java的版本

    [root@NB ok]# yum -y list java* Loaded plugins: fastestmirror, refresh-packagekit, security Loading ...

  4. Android init.rc执行顺序

    转自:http://blog.csdn.net/kickxxx/article/details/7590665 1. 所有的action运行于service之前 2.  下面为各个section的执行 ...

  5. sql server 用户'sa'登录失败(错误18456)(转载)

    转载地址:http://thenear.blog.51cto.com/4686262/865544 用户'sa'登录失败(错误18456)解决方案图解 当我们在使用sql server 的时候可能会遇 ...

  6. Delphi中的函数指针判断是否为空

    delphi函数指针 只有@@p才代表了函数指针本身的地址   assigned(p) 判断是否为空 或者用 @p=nil 来判断函数指针是不是为空 Delphi中的函数指针实际上就是指针,只是在使用 ...

  7. Delphi多线程开发注意事项

    Q1: 多线程中需避免多个线程同时向全局变量进行写入操作,导致访问冲突问题. A1:  可以通过使用加锁机制(比如:临界区.互斥.信号量)解决此问题. Q2:多线程中对于结构体和CLASS类型的全局变 ...

  8. 【PHP构造方法和析构方法的使用】

    构造方法:__construct,析构方法:__destruct 代码示例: <?php class Person { public $name; public $age; public fun ...

  9. golang 索引

    入门的基础路线 a Tour of GoEffective GoGo By Example 以上的三部分通读算是入门. 4个重要的组成部分 1. 基础知识2. 并发特性3. 异常处理4. 常用开源项目 ...

  10. Auto Layout

    Auto Layout XCode5+ Auto Layout Concepts 核心的概念是约束. Constraint Basics Constant value Relation Priorit ...