How to Get Current User Details in JavaScript

Introduction

 
In this article, I will explain how to access the current user's details. We can get userip, usercountry, usercity, userregion, userlatitude, userlongitude and usertimezone about the user. The "smart-ip.net" JSON string is used to access all the information about the user.
 
Complete Program
 
User_Infromation.html
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title></title>  
  5.     <script src="jquery-1.8.2.js"></script>  
  6.      
  7.  <script type="text/javascript">  
  8.      var userip, usercountry, usercity, userregion, userlatitude, userlongitude, usertimezone  
  9.      function UserInfo(data)  
  10.      {  
  11.          userip = data.host;  
  12.          usercountry = data.countryName;  
  13.          usercity = data.city;  
  14.          userregion = data.region;  
  15.          userlatitude = data.latitude;  
  16.          userlongitude = data.longitude;  
  17.          usertimezone = data.timezone;  
  18.      }  
  19.      $(function ()  
  20.      {  
  21.          UserInfoDetail();  
  22.      })  
  23.      function UserInfoDetail() {  
  24.          document.getElementById('userip').innerHTML = userip;  
  25.          document.getElementById('city').innerHTML = usercity;  
  26.          document.getElementById('country').innerHTML = usercountry;  
  27.           
  28.          document.getElementById('region').innerHTML = userregion;  
  29.          document.getElementById('timezone').innerHTML = usertimezone;  
  30.          
  31.          document.getElementById('longitude').innerHTML = userlongitude;  
  32.          document.getElementById('latitude').innerHTML = userlatitude;  
  33.           
  34.      }  
  35. </script>  
  36.     <script type="text/javascript" src="http://smart-ip.net/geoip-json?callback=UserInfo"></script>  
  37. </head>  
  38. <body>  
  39. <div>  
  40. <table id="Details" cellpadding="2" cellspacing="2" style=" border:1px solid #000; font-family:'Segoe UI';" >  
  41. <tr style="background-color:#666666; color:White; font-weight:bold">  
  42. <td colspan="2" align="center">Current User Details in JavaScript</td>  
  43. </tr>  
  44. <tr style="border:solid 1px #000000">  
  45. <td align="left"><b>IP Address  :</b></td>  
  46. <td><label id="userip"/></td>  
  47. </tr>  
  48.  <tr>  
  49. <td align="left"><b>City  :</b></td>  
  50. <td><label id="city"/></td>  
  51. </tr>  
  52. <tr>  
  53. <td align="left"><b>Country  :</b></td>  
  54. <td><label id="country"/></td>  
  55. </tr>  
  56. <tr>  
  57. <td align="left"><b>Region  :</b></td>  
  58. <td><label id="region"/></td>  
  59. </tr>  
  60. <tr>  
  61. <td align="left"><b>Time Zone  :</b></td>  
  62. <td><label id="timezone"/></td>  
  63. </tr>  
  64. <tr>  
  65. <td align="left"><b>Longitude  :</b></td>  
  66. <td><label id="longitude"/></td>  
  67. </tr>  
  68. <tr>  
  69. <td align="left"><b>latitude  :</b></td>  
  70. <td><label id="latitude"/></td>  
  71. </tr>  
  72. </table>  
  73. </div>  
  74. </body>  
  75. </html> 
Output 
 
Output.jpg

For more information, download the attached sample application.