Skip to content

Instantly share code, notes, and snippets.

@andrewbridge
Created May 17, 2019 09:50
Show Gist options
  • Save andrewbridge/c9388e9777f7522cacc0d0b16fd64d36 to your computer and use it in GitHub Desktop.
Save andrewbridge/c9388e9777f7522cacc0d0b16fd64d36 to your computer and use it in GitHub Desktop.
IE doesn't have the URL method, I needed to parse a URL with potential IE11 support. Polyfill the bare minimum of the method with regex. Doesn't support username:password pairs in URLs
if (typeof window.URL !== 'function') {
window.URL = function(url) {
var badUrl = new TypeError("Failed to construct 'URL': Invalid URL");
if (typeof url !== 'string') {
throw badUrl;
}
var match = url.match(/^(?<origin>(?<protocol>[^\/]+?)\/\/(?<host>(?<hostname>[^:]+)(:|)(?<port>[^\/]*?)))(?<pathname>\/[^\?\n]*)(?<search>(\?|)[^\n]*)/);
if (match === null) {
throw badUrl;
}
for (var key in match.groups) {
this[key] = match.groups[key];
}
this.href = url;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment