Skip to content

Instantly share code, notes, and snippets.

@nguyenning
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nguyenning/ac2b9449e87ef562261f to your computer and use it in GitHub Desktop.
Save nguyenning/ac2b9449e87ef562261f to your computer and use it in GitHub Desktop.
Checks if url is an external url or not.
var isExternalUrl = function (url) {
/* Tests if valid absolute url, ex:
* http://www.google.com | https://www.google.com | //www.google.com
*/
if (new RegExp('^((https?:)?//)', 'i').test(url)) {
var target = document.createElement('a');
target.href = url;
// note: hostname will be empty in IE for local urls
if (target.hostname.length > 0 &&
window.location.hostname.toLowerCase() !== target.hostname.toLowerCase()) {
return true;
}
}
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment