首页 > Web开发 > 详细

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

时间:2015-08-14 09:54:34      阅读:260      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!