Skip to content

Instantly share code, notes, and snippets.

@pixelsoul
Last active September 28, 2017 20:41
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 pixelsoul/5a102f48e89347d031e2385147609099 to your computer and use it in GitHub Desktop.
Save pixelsoul/5a102f48e89347d031e2385147609099 to your computer and use it in GitHub Desktop.
Parse url with JavaScript
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]] = decodeURIComponent(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