At many times we need to extract the root of a multi-level path URL. So here is the code to do that.
/*
@author Abhishek Kumar
@usage getBaseUrl(n); where n = number of ../
@example
For URL → http://localhost/workshop/showcase/shell/data/AKI/app/index.html
Result of
getBaseUrl() → http://localhost/workshop/showcase/shell/data/AKI/app/
getBaseUrl(3) → http://localhost/workshop/showcase/shell/
*/
function getBaseUrl(levelback) {
var basePath = window.location.pathname;
basePath = basePath.substring(0, basePath.lastIndexOf('/'));
var list = basePath.split('/');
for (var i = 0; i < levelback && list.length > 0; i++) {
list.pop();
}
return window.location.protocol + '//' + window.location.host + list.join('/') + '/';
}
Comments
Post a Comment