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

JS URL解析

时间:2014-08-18 15:59:12      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   for   ar   

  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解析,布布扣,bubuko.com

JS URL解析

标签:style   blog   http   color   os   io   for   ar   

原文地址:http://www.cnblogs.com/yzeng/p/3919534.html

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