Friday, 8 July 2011

Check any URL is exists/hosted or not Through JavaScript

function IsURLExists(url)
{
var req= new GetXMLHttpRequest(); // XMLHttpRequest object
      try
      {
            req.open("HEAD", url, false);
            req.send(null);        
            return req.status== 200 ? true : false;
      }
      catch (err) {
            return false;
}
}
       
function GetXMLHttpRequest()
{
var obj;
            
     if (window.XMLHttpRequest)
     {
           obj= new XMLHttpRequest();
     }
     else if (window.ActiveXObject)
     {
           try
           {
                   obj= new ActiveXObject('MSXML2.XMLHTTP.3.0');
           }
           catch(er)
           {
                 try
                 {
                         obj= new ActiveXObject("Microsoft.XMLHTTP");
                 }
                 catch(err)
                 {
                         obj= false;
                 }
           }
}

     return obj;
}

No comments:

Post a Comment