Skip to content

Instantly share code, notes, and snippets.

@ysakmrkm
Created August 16, 2012 03:48
Show Gist options
  • Save ysakmrkm/3366433 to your computer and use it in GitHub Desktop.
Save ysakmrkm/3366433 to your computer and use it in GitHub Desktop.
Get site URL
var href = location.href.split('/');
var path = '';
var localRegex = /^\d+\.\d+\.\d+\.\d+/; //Local IP Address
var workRegex = /^.*\/pc\/[^\/]+\/.*$/; //Change regex if you want.
for(i = 0 ; i < href.length ; i++){
if(href[i] == '' || i == href.length - 1 && href[i].indexOf('.') !== -1){
href.splice(i,1);
}
}
if(localRegex.test(location.hostname) === true || location.hostname.indexOf('localhost') !== -1){ //Local
var length = 2; //Change num if you want.
} else if(workRegex.test(location.href) === true){ //Work
var length = 3; //Change num if you want.
} else { //Production
var length = 1; //Change num if you want.
}
for(j = 0 ; j <= length ; j++){
path += href[j];
if(j == 0){
path += '//';
} else {
path += '/';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment