js获取url对象
2012-03-03 08:25
281 查看
function parseURL(url) {
var a = document.createElement('a');
//创建一个链接
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('/')
};
}
var a = document.createElement('a');
//创建一个链接
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('/')
};
}
相关文章推荐
- js获取url中的查询字符并返回对象格式
- js面向对象学习笔记之八(cookie使用 与 url的get参数获取)
- js 获取url传值和base64转成blob对象
- JS根据key值获取URL中的参数值及把URL的参数转换成json对象
- js获取url中的参数对象、js生成带参数的url
- JS根据key值获取URL中的参数值及把URL的参数转换成json对象
- JS根据key值获取URL中的参数值,以及把URL的参数转换成json对象
- Js 怎么遍历json对象所有key及根据动态key获取值
- asp.net、js中获取URL和IP地址方法大全
- JS 获取URL地址参数
- js页面传值乱码以及url获取方式
- JS获取URL中参数值(QueryString)的4种方法分享
- js解析URL为对象函数实现
- JS中关于页面URL地址的获取
- js文件封装javascript在html中获取url参数
- js 获取当前url
- JS获取function方法里定义的对象
- 两个很简单的js获取url参数的方法
- jsp页面中添加js脚本获取url参数