Parse URL using <a> element - Node.js DOM

Node.js examples for DOM:Element

Description

Parse URL using <a> element

Demo Code



var requestQueue = [];

function parseURL(url) {
    var a = document.createElement('a');
    a.href = url;/*from  w ww .  j av  a2s  . c  o m*/
    return {
        source: url,
        protocol: a.protocol.replace(':', ''),
        host: a.hostname,
        port: a.port,
        query: a.search,
        params: (function () {
            var ret = {};
            var seg = a.search.replace(/^\?/, '').split('&');
            for (var i = 0; i < seg.length; i++) {
                if (!seg[i]) {
                    continue;
                }
                var 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('/')
    };
}

Related Tutorials