Skip to content

Instantly share code, notes, and snippets.

@aNd1coder
Created April 14, 2016 07:32
Show Gist options
  • Save aNd1coder/8d493a1f4e436b233bf019718c60a4ca to your computer and use it in GitHub Desktop.
Save aNd1coder/8d493a1f4e436b233bf019718c60a4ca to your computer and use it in GitHub Desktop.
A useful javascript url parse function
function parseURL(url) {
var parser = document.createElement('a'),
searchObject = {},
queries, split, i;
// Let the browser do the work
parser.href = url;
// Convert query string to object
queries = parser.search.replace(/^\?/, '').split('&');
for( i = 0; i < queries.length; i++ ) {
split = queries[i].split('=');
searchObject[split[0]] = split[1];
}
return {
protocol: parser.protocol,
host: parser.host,
hostname: parser.hostname,
port: parser.port,
pathname: parser.pathname,
search: parser.search,
searchObject: searchObject,
hash: parser.hash
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment