I am using javascript to determine the visitors ip address. For what ever reason, it works in Chrome and not in Firefox, IE, or other browsers.
Here is my code:
function getIPAddress() {
var xmlHttp;
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.open("GET", "http://api.hostip.info/get_html.php", false);
xmlHttp.send();
var hostipInfo = xmlHttp.responseText.split("\n");
for (var i = 0; i < hostipInfo.length - 1; i++) {
var ipAddress = hostipInfo[i].split(":");
if (ipAddress[0] == "IP") return ipAddress[1];
}
return "unknown";
}
At the company I’m working for, I am behind a proxy. Could this be a proxy issue, or is there something wrong with this code? Thanks.
Just deployed my code to our test environment, and in IE, I receive a pop up saying ‘This page is accessing information that is not under its control. This poses a security risk. Do you want to continue?’ If I say, yes, it works. If I say, no, it doesn’t.
If you’re going to use AJAX (which is what this code is), I STRONGLY suggest you use a 3rd party wrapper like jQuery. That will increase cross-browser compatibility greatly and allows you to shrink your code down to something like this.
Additional Point
As
Pointymentioned, if your page is running on a different domain thanhostip.info, you will need to setup a local PHP to fetch the data like this..localGetData.php
New Ajax