Login Register
Code2night
  • Home
  • Guest Posts
  • Blog Archive
  • Tutorial
  • Languages
    • Angular
    • C
    • c#
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
    • SEO Analyzer
  1. Home
  2. Blog
  3. How to get visitors location like country and city

How to get visitors location like country and city

Date- Aug 31,2023

4146

Free Download Pay & Download
Visitor Location Browser Location

One popular option is the IP Geolocation API, which allows you to obtain geolocation information based on the user's IP address.It can be used for getting user details. Here's an example of how you can use the this API:

 <main>
   

    <div class="row">
        <section class="col-md-4" aria-labelledby="gettingStartedTitle">
            <h2 id="gettingStartedTitle">User Location and IP</h2>

            <p id="IP"></p>
            <p id="City"></p>
            <p id="Country"></p>
            <p id="currency"></p>
            <p id="timezone"></p>
        </section>
        
    </div>
</main>
<script>
     fetch('https://ipapi.co/json/')
                        .then(response => response.json())
                        .then(data => {

                            const city = data.city;
                            console.log(data);
                            
                            $('#IP').html("IP Address: " + data.ip);
                            $('#City').html("city: " + data.city);
                            $('#Country').html("Country: " + data.country_name);
                            $('#currency').html("currency: " + data.currency);
                            $('#timezone').html("timezone: " + data.timezone);

                        })
</script>

Now run the application and check the console tab after inspect element. You will be able to see following details

So, this is how  to get visitors location like country and city. And also we can get the user's ip address if we need.

In the code above, we use the IP Geolocation API provided by ipapi.co. The API endpoint 'https://ipapi.co/json/' returns JSON data containing geolocation information based on the user's IP address. We can extract the city information from the response and log it to the console or user it wherever we want .

One thing to be noted here is IP Geolocation api can have usage restriction for free quota . So don't forget to check the terms of usage and limits. And also results can vary in case of use of vpn's.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Realtime Speech to Text converter using javascript
Dec 16, 2023
flat() and flatMap() in JavaScript
May 19, 2023
Screen Recording with Audio using JavaScript in ASP.NET MVC
Mar 20, 2023
Countown Timer in Javascript
Sep 17, 2022

Comments

Contents

More in JavaScript

  • Slick Slider 14818 views
  • Card Number Formatting using jquery 11546 views
  • Alphanumeric validation in JavaScript 8762 views
  • Jquery Autocomplete 8375 views
  • Input Mask in Jquery 7444 views
View all JavaScript posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
  • SEO Analyzer
By Language
  • Angular
  • C
  • c#
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page