Finding the Contextpath from XMLHTTP Request

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jad2006
    New Member
    • Jan 2007
    • 7

    Finding the Contextpath from XMLHTTP Request

    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
  • mrhoo
    Contributor
    • Jun 2006
    • 428

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

    [CODE=javascript]pathTrack= function(url){
    if(url.indexOf( location.protoc ol)== 0 || /^(mailto|http)\ :/i.test(url)) return url;

    url= url.replace(/\\/g,'/');
    var Loc= location.href.r eplace(/\\/g,'/');
    var ax= Loc.indexOf(loc ation.pathname) ;
    while(Loc.charA t(ax)== '/')++ax;
    var Rte= Loc.substring(0 ,ax);

    if(/^\//.test(url)) return Rte + url;

    Loc= Loc.substring(0 ,Loc.lastIndexO f('/'));
    if(/^[^\.]/.test(url)) return Loc+'/'+url;

    var L= ax;
    while (url.indexOf('. ./')==0 ){
    ax= Loc.lastIndexOf ('/');
    Loc= Loc.substring(0 ,ax);
    url= url.substring(3 );

    if(ax< L) throw new Error('Bad path-'+Loc+'/'+url);
    }
    return Loc + '/' + url;
    }[/CODE]

    Comment

    • rnd me
      Recognized Expert Contributor
      • Jun 2007
      • 427

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


      Code:
      function qualifyPath(paf) {
          var i = new Image();
          i.src = paf;
          return i.src;
      }
      
      //example:
      
      alert (qualifyPath("localPage.htm"))
      alert (qualifyPath("../parentPage.htm"))

      Comment

      Working...