Skip to content

Instantly share code, notes, and snippets.

@ZoranJambor
Forked from jlong/uri.js
Last active September 28, 2016 13:46
Show Gist options
  • Save ZoranJambor/3937654 to your computer and use it in GitHub Desktop.
Save ZoranJambor/3937654 to your computer and use it in GitHub Desktop.
URI Parsing with Javascript
/**
* Parse URL
*
* @param {String} url
* @return {Object}
*/
parseURL = function(url) {
var el = document.createElement("a");
el.href = url || document.location.href;
// var url = "http://domain.com:8080/path/to/somewhere/?foo=bar#hash"
return {
protocol : el.protocol, // "http:"
hostname : el.hostname, // "domain.com"
port : el.port || "80", // "8080"
pathname : el.pathname, // "/path/to/somewhere/"
search : el.search, // "?foo=bar"
hash : el.hash, // "#hash"
host : el.host // "domain.com:8080"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment