Connecting Tech Pros Worldwide Help | Site Map

Finding the Contextpath from XMLHTTP Request

Newbie
 
Join Date: Jan 2007
Posts: 7
#1: Mar 4 '08
Hi
We tried creating AJAX based test kit, It will prompt for a URL to be tested. It will analyse all the links in the URL and trigger ajax calls to each and every link in the page and it will check the status of the links and update the same. This worked fine with absolute urls, but if there are relative urls in the page its not able to properly construct the url since it does not know the context path
Is there any way by which we could find out the ContextPath of the XmlHTTPRequest
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#2: Mar 4 '08

re: Finding the Contextpath from XMLHTTP Request


This may be useful to resolve relative paths to absolute paths, in the context of the current location.

Expand|Select|Wrap|Line Numbers
  1. pathTrack= function(url){    
  2.     if(url.indexOf(location.protocol)== 0 || /^(mailto|http)\:/i.test(url)) return url;
  3.  
  4.     url= url.replace(/\\/g,'/');
  5.     var Loc= location.href.replace(/\\/g,'/');
  6.     var ax= Loc.indexOf(location.pathname);
  7.     while(Loc.charAt(ax)== '/')++ax;
  8.     var Rte= Loc.substring(0,ax);
  9.  
  10.     if(/^\//.test(url)) return Rte + url;
  11.  
  12.     Loc= Loc.substring(0,Loc.lastIndexOf('/'));
  13.     if(/^[^\.]/.test(url)) return Loc+'/'+url;
  14.  
  15.     var L= ax;
  16.     while (url.indexOf('../')==0 ){
  17.         ax= Loc.lastIndexOf('/');
  18.         Loc= Loc.substring(0,ax);
  19.         url= url.substring(3);
  20.  
  21.         if(ax< L) throw new Error('Bad path-'+Loc+'/'+url);
  22.     }
  23.     return Loc + '/' + url;
  24. }
rnd me's Avatar
Expert
 
Join Date: Jun 2007
Location: Urbana IL
Posts: 411
#3: Mar 4 '08

re: Finding the Contextpath from XMLHTTP Request


here is a simple, rock solid way to qualify paths;


Expand|Select|Wrap|Line Numbers
  1. function qualifyPath(paf) {
  2.     var i = new Image();
  3.     i.src = paf;
  4.     return i.src;
  5. }
  6.  
  7. //example:
  8.  
  9. alert (qualifyPath("localPage.htm"))
  10. alert (qualifyPath("../parentPage.htm"))
  11.  
Reply