Skip to content

Instantly share code, notes, and snippets.

@JamesMGreene
Created September 18, 2012 14:20
Show Gist options
  • Save JamesMGreene/3743404 to your computer and use it in GitHub Desktop.
Save JamesMGreene/3743404 to your computer and use it in GitHub Desktop.
Break a URL down by utilizing an HTML anchor ("a") element to turn it into a Location object
var getUrlAsLocation = (function() {
var urlPartKeyNames = ["href", "protocol", "host", "hostname", "port", "pathname", "search", "hash"];
return function(url) {
var link = document.createElement("a"),
urlAsLocation = {};
link.href = url;
for (var i = 0, len = urlPartKeyNames.length; i < len; i++) {
var urlPartKey = urlPartKeyNames[i];
urlAsLocation[urlPartKey] = link[urlPartKey];
}
link = null;
return urlAsLocation;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment