Monday, February 26, 2007

Get page output by submitting URL using Javascript

var requestURL = http://api.hostip.info/?ip=;

function GetLocation4IP(str)
{
requestURL=requestURL+str;
var xmlhttp=false;
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (E)
{
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined')
{
xmlhttp = new XMLHttpRequest();
}
xmlhttp.open("GET",requestURL,true);
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
//alert(xmlhttp.responseText);
// code for IE
if (window.ActiveXObject)
{
var doc=new ActiveXObject("Microsoft.XMLDOM");
doc.async="false";
doc.loadXML(xmlhttp.responseText);
}
// code for Mozilla, Firefox, Opera, etc.
else
{
var parser=new DOMParser();
var doc=parser.parseFromString(xmlhttp.responseText,"text/xml");
}// documentElement always represents the root node
var myXML=doc.documentElement;
var city=myXML.childNodes[3].childNodes[0].childNodes[0].childNodes[0].nodeValue;
var country=myXML.childNodes[3].childNodes[0].childNodes[1].childNodes[0].nodeValue;
var coord;
var longitude="";
var latitude="";
var display;
//display=city+""+country;
if(city!="(Private Address)")
{
coord=myXML.childNodes[3].childNodes[0].childNodes[4].childNodes[0].childNodes[0].childNodes[0].childNodes[0].nodeValue;
var coordinates=coord.split(',')
longitude=coordinates[0];
latitude=coordinates[1];
//display+=""+longitude+""+latitude;
}
display=city+""+country+""+longitude+""+latitude;
document.write(display);
}
}
xmlhttp.send(null);
}