DEV Community 👩‍💻👨‍💻

Discussion on: How to get user IP address, location, and device info using JavaScript in browser

Collapse
 
note_info_web profile image
Note Info • Edited on

This is work for me

// Ditect user info
let Url = "https://" + "cloudflare.com/cdn-cgi/trace";
let AjaxUrl = new XMLHttpRequest();
AjaxUrl.open("get", Url);
AjaxUrl.send();

AjaxUrl.onreadystatechange = function () {
if (AjaxUrl.readyState === 4 && AjaxUrl.status === 200) {
let resultUrl = AjaxUrl.responseText;

let mapUrlStart = resultUrl.indexOf("ip") + 3;
let mapUrlEnd = resultUrl.indexOf("ts");
let IpResult = resultUrl.slice(mapUrlStart, mapUrlEnd);
console.log(IpResult);
Enter fullscreen mode Exit fullscreen mode

}
};

By = Note Info

Collapse
 
ordinz profile image
Ordin

wow that's pretty awesome, thank you!