I want to get the location and the first folder, like:
http://www.example.com/test/
var $location = window.location.href;
alert ($location);
this returns
http://www.example.com/test/location-test.html
So I would like it to return everything up to "test/", so I only end up with:
http://www.example.com/test/
Thanks in advance!
You can try this.
var url = location.protocol + "//" + document.domain + "/"
+ location.pathname.split('/')[1] + "/";
Try this
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;
}
Handles almost all the cases { I guess so :) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With