Get User Details in TypeScript

Introduction

In this article I will explain how to access the current user's details in TypeScript. We can get the IP, country, city, region, latitude, longitude and timezone of the user. The "smart-ip.net" JSON provides all the information about the user.

Use the following procedure.

Step 1

Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is opened. In this window, click "HTML Application for TypeScript" under Visual C#.

Give the name of your application as "Get_User_Detail" and then click "Ok".

Step 2

After this session the project has been created. A new window is opened on the right side. This window is called the Solution Explorer. The Solution Explorer contains the ts file, js file and css files and the aspx page.

 solution-explorer.jpg

Program Coding

UserDetails.ts

class User_Details

{

    UserInfo(data)

    {

        var ip, country, city, region, latitude, longitude, timezone

        ip = data.host;

        country = data.countryName;

        city = data.city;

        region = data.region;

        latitude = data.latitude;

        longitude = data.longitude;

        timezone = data.timezone;

 

        document.getElementById('userip').innerHTML = ip;

        document.getElementById('city').innerHTML = city;

        document.getElementById('country').innerHTML = country;

 

        document.getElementById('region').innerHTML = region;

        document.getElementById('timezone').innerHTML = timezone;

 

        document.getElementById('longitude').innerHTML = longitude;

        document.getElementById('latitude').innerHTML = latitude;

    }

}

window.onload = () =>

{

 

    var obj = new User_Details();

    obj.UserInfo({ "source": "smart-ip.net",

                   "host": "192.1.1.254",

                   "lang": "en", "countryName": "India",

                   "countryCode": "IN", "city": "Noida",

                   "region": "Uttar Pradesh",

                   "latitude": "15.40227", "longitude": "48.2222",

                   "timezone": "Asia\/Delhi"

    });

};

 

default.htm

<!DOCTYPE html>

 

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>

    <meta charset="utf-8" />

    <title>TypeScript HTML App</title>

    <link rel="stylesheet" href="app.css" type="text/css" />

    <script src="UserDetails.js"></script>

<script type="text/javascript" src="http://smart-ip.net/geoip-json?callback=UserInfo"></script>

</head>

<body>

<div>

<table id="Details" cellpadding="2" cellspacing="2" style=" border:1px solid #000; font-family:'Segoe UI';" >

<tr style="background-color:cornflowerblue; color:White; font-weight:bold">

<td colspan="2" align="center">Current User Details in TypeScript</td>

</tr>

<tr style="border:solid 1px #000000">

<td align="left" style="color:blueviolet"><b>IP Address  :</b></td>

<td><label id="userip"/></td>

</tr>

 <tr>

<td align="left"><b>City  :</b></td>

<td><label id="city"/></td>

</tr>

<tr>

<td align="left" style="color:blueviolet"><b>Country  :</b></td>

<td><label id="country"/></td>

</tr>

<tr>

<td align="left"><b>Region  :</b></td>

<td><label id="region"/></td>

</tr>

<tr>

<td align="left" style="color:blueviolet"><b>Time Zone  :</b></td>

<td><label id="timezone"/></td>

</tr>

<tr>

<td align="left"><b>Longitude  :</b></td>

<td><label id="longitude"/></td>

</tr>

<tr>

<td align="left" style="color:blueviolet"><b>latitude  :</b></td>

<td><label id="latitude"/></td>

</tr>

</table>

</div>

</body>

</html>

    

UserDetails.js

 

var User_Details = (function () {

    function User_Details() { }

    User_Details.prototype.UserInfo = function (data) {

        var ip, country, city, region, latitude, longitude, timezone;

        ip = data.host;

        country = data.countryName;

        city = data.city;

        region = data.region;

        latitude = data.latitude;

        longitude = data.longitude;

        timezone = data.timezone;

        document.getElementById('userip').innerHTML = ip;

        document.getElementById('city').innerHTML = city;

        document.getElementById('country').innerHTML = country;

        document.getElementById('region').innerHTML = region;

        document.getElementById('timezone').innerHTML = timezone;

        document.getElementById('longitude').innerHTML = longitude;

        document.getElementById('latitude').innerHTML = latitude;

    };

    return User_Details;

})();

window.onload = function () {

    var obj = new User_Details();

    obj.UserInfo({

        "source": "smart-ip.net",

        "host": "192.1.1.254",

        "lang": "en",

        "countryName": "India",

        "countryCode": "IN",

        "city": "Noida",

        "region": "Uttar Pradesh",

        "latitude": "15.40227",

        "longitude": "48.2222",

        "timezone": "Asia\/Delhi"

    });

};

//@ sourceMappingURL=UserDetails.js.map

 

Output

 

Output.jpg

For more information, download the attached sample application.

Up Next
    Ebook Download
    View all
    Test
    Read by 16 people
    Download Now!
    Learn
    View all