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解析
时间: 2024-11-05 21:40:27