Skip to content

Instantly share code, notes, and snippets.

@akhr
Created August 11, 2015 20:52
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 akhr/5d520ab54687532a7b69 to your computer and use it in GitHub Desktop.
Save akhr/5d520ab54687532a7b69 to your computer and use it in GitHub Desktop.
function serverUrlValidator(input){
var url = input.toLowerCase();
var arr = url.split('://');
var protocol = arr[0];
if(protocol === 'https' || protocol === 'http'){
//Do nothing
}else if(protocol === url){
url = 'https://' + url;
}
var parser = document.createElement('a');
parser.href = url;
var protocol = parser.protocol;
var host = parser.host;
var domain = parser.domainname;
var port = parser.port;
var pathname = parser.pathname;
var serverUrl = protocol + '//' + host + pathname;
console.log('Built protocol : '+protocol);
console.log('Built domain : '+domain);
console.log('Built host : '+host);
console.log('Built port : '+port);
console.log('Built pathname : '+pathname);
if(host){
if(protocol === 'https:'){
console.log('AWESOME - URL --> '+serverUrl);
}else if(protocol === 'http:'){
console.log('WARNING : HTTP - URL --> '+serverUrl);
}else{
console.log('ERROR : INVALID protocol');
}
}else{
console.log('ERROR : INVALID URL');
}
}
serverUrlValidator('sample.com:9000/lkjsdhf.txt')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment