Skip to content

Instantly share code, notes, and snippets.

@andfinally
Created October 8, 2013 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andfinally/6882904 to your computer and use it in GitHub Desktop.
Save andfinally/6882904 to your computer and use it in GitHub Desktop.
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;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment