`
seavers
  • 浏览: 170351 次
  • 来自: ...
社区版块
存档分类
最新评论

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[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;
}






分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics