JS URL解析

  function urlParse(url) {
        try {
            var hostexp = /http\:\/\/([^\/]+)/;
            var hoststr = url.match(hostexp)[0];
            var hosttmp = hoststr.replace("http://", "").split(":");
            var pathtmp = url.replace(hoststr, "").split("?");
            var urlObj = {};
            urlObj.host = hosttmp[0];
            urlObj.port = hosttmp.length > 1 ? hosttmp[1] : 80;
            urlObj.path = pathtmp.length > 0 ? pathtmp[0] : "";
            urlObj.params = {};
            urlObj.paramsStr = "";
            if (pathtmp.length > 1) {
                urlObj.paramsStr = pathtmp[1];
                var params = pathtmp[1].split("&");
                for (var i = 0; i < params.length; i++) {
                    var parr = params[i].split("=");
                    if (urlObj.params[parr[0]]) {
                        if (typeof urlObj.params[parr[0]] != "object") {
                            var v = urlObj.params[parr[0]];
                            urlObj.params[parr[0]] = [];
                            urlObj.params[parr[0]].push(v);
                        }
                        urlObj.params[parr[0]].push(parr[1]);
                    }
                    else
                        urlObj.params[parr[0]] = parr[1];
                }
            }
            return urlObj;
        } catch (exception) { return null; }
    }

演示下载

JS URL解析,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。