Friday, January 7, 2011

Getting Alexa Rank Using JavaScript..

 Here I am making simple java script function to find out alexa rank : 


function getAlexaRank(URL)
{     
      var currentURLArray = URL.split('/');
       var alexaURL = "http://data.alexa.com/data?cli=10&dat=s&url=" + currentURLArray[2];
       var xmlhttp = new XMLHttpRequest();
   
       xmlhttp.open("GET", alexaURL, true); 
  
       xmlhttp.onreadystatechange = function()
       {
          if (xmlhttp.readyState == 4)
          {           
              var response = xmlhttp.responseText;           
              //alert("Alexa Rank " + response); 
              var firstindex = response.indexOf("REACH RANK");
              var lastindex = response.indexOf("RANK DELTA");
              //alert("First Index=" + firstindex + "Last Index" + lastindex);
              response = response.substring(firstindex,lastindex-4);             
              response = response.split('"');
              if(firstindex == -1 && lastindex == -1)
                alert("Alexa Rank not found");
              else
                 alert("Alexa Rank=" + response[1]);
           
          }
       }   
       xmlhttp.send(null);     
}

Hope this will be helpful for you.

No comments:

Post a Comment