Created
September 18, 2012 14:20
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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