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.
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
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"
- });
- };
Output
For more information, download the attached sample application.