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

JS 解析 URL 代码

时间:2020-03-15 23:54:24      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:win   ram   ret   item   relative   protocol   name   var   arch   

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 = {};
            var seg = window.location.search.replace(/^\?/, '').split('&');
            var s;
            seg.forEach(function (item, index) {
                if (!item) {
                    return;
                }
                s = seg[index].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 代码

标签:win   ram   ret   item   relative   protocol   name   var   arch   

原文地址:https://www.cnblogs.com/everlose/p/12501159.html

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