Created
July 15, 2013 11:30
-
-
Save laurent22/5999289 to your computer and use it in GitHub Desktop.
Extracts base URL from location of current document.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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