码迷,mamicode.com
首页 > Web开发 > 详细

[前端]利用a标签获取url里所需的内容

时间:2015-08-14 09:55:01      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

// This function creates a new anchor element and uses location
// properties (inherent) to get the desired URL data. Some String
// operations are used (to normalize results across browsers).
 
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(‘/‘)
    };
}

  来源:http://james.padolsey.com/javascript/parsing-urls-with-the-dom/

[前端]利用a标签获取url里所需的内容

标签:

原文地址:http://www.cnblogs.com/yanyd/p/4729063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!