Попробуйте это
function baseUrl() {
var baseUri = window.location.href;
//Removes any # from href (optional)
if(baseUri.slice(baseUri.length - 1, baseUri.length) == "#")
baseUri = baseUri.slice(0, baseUri.length - 1);
var split = window.location.pathname.split('/');
if (split.length > 2)
baseUri = baseUri.replace(window.location.pathname, '') + "/" + split[1] + "/";
//This will append the last slash
if (baseUri.substring(baseUri.length - 1, baseUri.length) != "/")
baseUri += "/";
return baseUri;
}
Обрабатывает почти все случаи {я так думаю :)}