Convert relative URL to absolute URL? 
Author Message
 Convert relative URL to absolute URL?

Is there a simple way to convert a relative URL to an absolute URL?

    Example: ".../myfolder/mydoc.asp" to "http://myweb/myfolder/mydoc.asp"

Thanks

--
Dave Veeneman
Chicago



Tue, 22 Mar 2005 21:13:36 GMT  
 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:
}



Wed, 23 Mar 2005 07:06:40 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Problems with using document.write and a relative URL

2. how to use url.Encode for NS problem w/ params in URL

3. URL, Getting the url using asp

4. WSH that makes a url file Makes BlueDragon.url

5. Extracting the URL from a url-file

6. Problem: Setting MSIE iframe innerHTML change relative href/src to absolute href/src

7. Absolute vs. Relative

8. Slash/Backslash, Absolute/Relative Path in FSO

9. OT: Relative/Absolute Paths to files

10. Converting URL string to Hyperlink

11. Relative paths MUST STAY RELATIVE!!!

12. How can I get the url name that I clicked on the click event of that url.

 

 
Powered by phpBB® Forum Software