Skip to content

Instantly share code, notes, and snippets.

@laurent22
Created July 15, 2013 11:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laurent22/5999289 to your computer and use it in GitHub Desktop.
Save laurent22/5999289 to your computer and use it in GitHub Desktop.
Extracts base URL from location of current document.
// Extracts base URL from location of current document.
function extractBaseUrl() {
// Get URL without query path
var url = [location.protocol, '//', location.host, location.pathname].join('');
// Remove filename if present
var f = url.substr(url.lastIndexOf("/") + 1).toLowerCase();
if (f.indexOf('.html') >= 0 || f.indexOf('.htm') >= 0 || f.indexOf('.php')) {
url = url.substr(0, url.length - f.length);
}
// Add last slash if missing
if (url == '' || url[url.length - 1] != '/') url += '/';
return url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment