
Convert relative URL to absolute URL?
Here's the code I wrote to do the job. It's quick-and-dirty-- I suspect
there is a more elegant way to skin the cat...
/* The getAbsoluteURL() function converts a relative URL to an absolute URL.
*/
function getAbsoluteURL(relativeURL,makeSSL) {
// Set the scheme
if (makeSSL) {
var scheme = "https://";
}
else {
var scheme = "http://";
}
// Set the host domain and the path
var hostDomain = Request.ServerVariables("SERVER_NAME").item;
var path = Request.ServerVariables("URL").item;
// Build full URL to current page
var currentURL = scheme + hostDomain + path;
// Initialize 'step back' search
var foundAt = -3;
var stepBackCount = -1;
// Determine how many levels we need to 'step back' in the URL
while (foundAt != -1) {
stepBackCount++
var urlEnd = relativeURL.substr(foundAt + 3);
foundAt = relativeURL.indexOf("../", foundAt + 3);
}
// Perform step-back in base URL
var baseEnd = currentURL.length + 1;
for (var i = 0; i < stepBackCount; i++) {
// 'Step back' to previous folder slash
baseEnd = currentURL.lastIndexOf("/", baseEnd);
// If we have run out of folder slashes, throw an error
if (baseEnd == -1) {
errMsg = "Function: getAbsoluteURLFromRelative(); Error: "
+ "Can't resolve relative URL from this location."
throw new Error(errMsg);
}
}
// Truncate URL base
var urlBase = currentURL.substr(0,baseEnd + 1);
// Append URL end
var absoluteURL = urlBase + urlEnd;
// Set return value
return absoluteURL;
Quote:
}