Skip to content

Instantly share code, notes, and snippets.

@gilly3
Last active August 29, 2015 14:06
Show Gist options
  • Save gilly3/8653b5cf9622b598625e to your computer and use it in GitHub Desktop.
Save gilly3/8653b5cf9622b598625e to your computer and use it in GitHub Desktop.
URL parsing utilities - Provides a shim for IE, where window.URL has a different meaning than in Chrome and Firefox
function _URL(url) {
// Naive URL parser. Assumes the input URL is valid.
this.hash = "";
this.search = "";
this.pathname = "";
this.port = "";
this.hostname = "";
this.host = "";
this.password = "";
this.username = "";
this.protocol = "";
this.origin = "";
this.href = url;
var hashIdx = url.indexOf("#");
if (hashIdx > -1) {
this.hash = url.slice(hashIdx);
url = url.slice(0, hashIdx);
}
var searchIdx = url.indexOf("?");
if (searchIdx > -1) {
this.search = url.slice(searchIdx);
url = url.slice(0, searchIdx);
}
var protoIdx = url.indexOf("://") + 1;
this.protocol = url.slice(0, protoIdx);
url = url.slice(protoIdx + 2);
var slashIdx = url.indexOf("/");
if (slashIdx > -1) {
this.host = url.slice(0, slashIdx);
this.pathname = url.slice(slashIdx);
}
else {
this.host = url;
}
var userIdx = this.host.indexOf("@");
if (userIdx > -1) {
this.username = this.host.slice(0, userIdx);
this.host = this.host.slice(userIdx + 1);
}
var pwdIdx = this.username.indexOf(":");
if (pwdIdx > -1) {
this.password = this.username.slice(pwdIdx + 1);
this.username = this.username.slice(0, pwdIdx);
}
var portIdx = this.host.indexOf(":");
if (portIdx > -1) {
this.hostname = this.host.slice(0, portIdx);
this.port = this.host.slice(portIdx + 1);
}
else {
this.hostname = this.host;
}
this.origin = this.protocol + "//" + this.host;
}
function parseUrlArgs(argString) {
// Decode key value pairs in url.search or url.hash into an Object
// Values are all strings.
// Values for duplicate keys are wrapped in arrays.
// Values for keys with '=' but no value (eg, &key=&) are empty strings.
// Values for keys without '=' (eg, &key&) are null.
// This function expects a URI Encoded string including the leading ? or #.
// So standard strings can be passed, eg, location.search, URI.hash, etc.
var args = {};
argString.slice(1).split("&").forEach(function (arg) {
if (!arg) return;
var parts = arg.split("=");
var key = decodeURLComponent(parts[0]);
var val = parts.length > 1 ? decodeURLComponent(parts[1]) : null;
if (key in args) {
if (!(args[key] instanceof array)) {
args[key] = [args[key]];
}
args[key].push(val);
}
else {
args[key] = val;
}
});
return args;
}
function decodeURLComponent(s) {
// interpret + in URLs as space
return decodeURIComponent(s.replace(/\+/g, " "));
}
function encodeURLComponent(s) {
// encode space in URLs with +
return encodeURIComponent(s).replace(/%20/g, "+");
}
// Using typeof because window.URL is defined in IE10+, but as an object, not a function
var URL = typeof window.URL === "function" ? window.URL : _URL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment