当前位置: 代码迷 >> 综合 >> parseURL
  详细解决方案

parseURL

热度:1   发布时间:2024-01-16 15:35:22.0
我们知道, 在location对象中, 可以直接获取 hostname, search , hash 等参数
而链接的url中, 我们没有现成的API可以获取这些参数
下面是一个小方法, 可以得到链接中的各种参数



function parseURL(url) {
var ret = {};
ret.href = url;

var match = url.match(/^([^\/:]+:)?(?:\/\/([^\/:]*)(?::([\d]+))?)?(\/?[^\?#]*)?(\?[^\#]*)?(#.*)?$/i);
if (match) {
ret.isValid = true;
ret.protocol = match[1] || '';
ret.hostname = match[2] || '';
ret.port = match[3] || '';
ret.host = ret.hostname + ret.port ? ':' : '' + ret.port;
ret.pathname = match[4] || "/";
ret.origin = ret.protocol + ret.host ? '//' : '' + ret.host;
ret.search = match[5] || '';
ret.hash = match[6] || '';
} else {
ret.isValid = false;
ret.path = ret.url;
}
return ret;
}
  相关解决方案