Skip to content

Instantly share code, notes, and snippets.

@netsi1964
Last active October 30, 2019 08:59
Show Gist options
  • Save netsi1964/971f49e1f4300aff0b1861d4f7fe20dd to your computer and use it in GitHub Desktop.
Save netsi1964/971f49e1f4300aff0b1861d4f7fe20dd to your computer and use it in GitHub Desktop.
Vanilla javascript XHR request to ipinfo.io
function ipinfo(cb, ip) {
var request;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
request = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
try {
request = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try {
request = new ActiveXObject('Microsoft.XMLHTTP');
} catch (e) {}
}
}
request.open("GET", '//ipinfo.io/' + ((ip) ? ip : ''), false);
request.setRequestHeader("Accept", "application/json");
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
cb(request.responseText);
}
};
request.send(null);
}
ipinfo(function(response) {
console.log(response);
});
@Leedehai
Copy link

Hi.. Chrome alerts "Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment