andfinally
10/8/2013 - 10:49 AM

Turn a URL into just the path + querystring

Turn a URL into just the path + querystring

// normalizeUrl('http://news.bbc.co.uk/sport/?page=1#test');
// returns "/sport/?page=1"

var normalizeUrl = function (url) {
	var a = document.createElement('a');
	a.href = url;
	a = a.pathname + a.search;
	a = a.indexOf('/') === 0 ? a : '/' + a; // because IE doesn't return a leading '/'
	return a;
};