What\'s the fastest method to detect if foo=\'http://john.doe\'
is an external url (in comparsion to window.location.href
)?
The main problem, is how to parse an URL, and get a host name our of it. It can be done with following way:
var _getHostname = function(url) {
var parser = document.createElement('a');
parser.href = url;
return parser.hostname;
}
var isExternal = (_getHostname(window.location.href) !== _getHostname('http://john.doe'));
Or you can use is-url-external module.
var isExternal = require('is-url-external');
isExternal('http://john.doe'); // true | false