VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > JavaScript教程 >
  • 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; }
    }
复制代码

演示下载

致读者:感谢你阅读本文,请随手点击右下角的推荐或分享,谢谢!


出处:
https://www.cnblogs.com/yzeng/p/3919534.html

相关教程