I was looking for a way to get the root domain name of any URL, through use of JavaScript. I often use Cassani for local development and run into the issue of the port and application name when trying to set the image location to a folder off the root such as: “lightbox/images”.
I updated a function that I use to consider if the app is running in Cassani or not. If not, just grab the URL. If it is running on localhost, include the application name too.
function getRootURL() {
var baseURL = location.href;
var rootURL = baseURL.substring(0, baseURL.indexOf('/', 7));
// if the root url is localhost, don't add the directory as cassani doesn't use it
if (baseURL.indexOf('localhost') == -1) {
return rootURL + "/";
} else {
return rootURL + baseURL.substring(baseURL.indexOf('/', 8), baseURL.indexOf('/', baseURL.indexOf('/', 8)+1)) + "/";
}
}
A little environment transparency.