Last active
August 29, 2015 14:17
-
-
Save JvanderHeide/a7405e5e58c32c9537c5 to your computer and use it in GitHub Desktop.
JavaScript String/URL methods
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 String.prototype.startsWith != 'function') { | |
String.prototype.startsWith = function (str){ | |
return this.slice(0, str.length) == str; | |
}; | |
} | |
if (typeof String.prototype.toURL != 'function') { | |
String.prototype.toURL = function () { | |
var parser = document.createElement('a'); | |
parser.href = this; | |
return parser; | |
}; | |
} | |
/* IE Doesn't have window.location.origin by default, this snippet fixes that. | |
* @link http://tosbourn.com/a-fix-for-window-location-origin-in-internet-explorer/ | |
*/ | |
if (!window.location.origin) { | |
window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: ''); | |
} | |
if (typeof String.prototype.isInternalURL != 'function') { | |
String.prototype.isInternalURL = function() { | |
var parser = this.toURL(), | |
href = parser.href, | |
location = window.location.origin; | |
return href.startsWith(location); | |
}; | |
} | |
if (typeof String.prototype.URLHasHash != 'function') { | |
String.prototype.URLHasHash = function () { | |
var parser = this.toURL(), | |
hash = parser.hash; | |
return ( hash.length > 0 ); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment