seavers 发表于 2013-1-29 08:41:59

parseURL

我们知道, 在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 || '';ret.hostname = match || '';ret.port = match || '';ret.host = ret.hostname + ret.port ? ':' : '' + ret.port;ret.pathname = match || "/";ret.origin = ret.protocol + ret.host ? '//' : '' + ret.host;ret.search = match || '';ret.hash = match || '';} else {ret.isValid = false;ret.path = ret.url;}return ret;}
页: [1]
查看完整版本: parseURL