javascript url 解析函数
日期:2010-07-04 阅读:158 分类:Javascript
Code:
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
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('/')
};
}
UseAge:
var myURL = parseURL('http://abc.com:8080/dir/index.html?id=255&m=hello#top');
myURL.file; // = 'index.html'
myURL.hash; // = 'top'
myURL.host; // = 'abc.com'
myURL.query; // = '?id=255&m=hello'
myURL.params; // = Object = { id: 255, m: hello }
myURL.path; // = '/dir/index.html'
myURL.segments; // = Array = ['dir', 'index.html']
myURL.port; // = '8080'
myURL.protocol; // = 'http'
myURL.source; // = 'http://abc.com:8080/dir/index.html?id=255&m=hello#top'
from:
相关文章
- JS和VML画曲线图 2008-07-06
- 支持IE/Firefox复制的JS代码 2008-12-15
- css+js无限级菜单 2008-11-17
- js中的dateAdd和dateDiff 2008-11-16
- url与uri的区别 2008-12-11
- 请求筛选模块被配置为拒绝包含hiddenSegment节的URL中的路径 2011-07-28
- Javascript URL编码方法的比较 2008-09-02
- 在Windows下使用curl命令 2009-03-23
- URL格式规范 2008-12-17
- Javascript常用函数归档 2008-12-01
- 过滤html Tags函数 2008-11-23
- 非常简练的ajax操作函数 2008-10-28
- JavaScript中isPrototypeOf函数 2009-01-30
- JavaScript和swf中函数调用 2009-02-26