Created
May 17, 2019 09:50
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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