Last active
August 29, 2015 14:04
-
-
Save nguyenning/ac2b9449e87ef562261f to your computer and use it in GitHub Desktop.
Checks if url is an external url or not.
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
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