Daniel - September 7, 2020

How To Get IP Address In JavaScript

In this post, we will show you how to get an IP address in JavaScript.

As you already know, JavaScript is currently the most popular programming language in the world. It is a high-level language but almost ironically, it is very easy to learn.

Interestingly, several people look to uncover IP addresses using JavaScript.

Although, there are different platforms where you can look up IP addresses, but knowing how to do so with JavaScript will help to improve your programming skills.

So how do you get an IP address in JavaScript? You will discover that in this post. But, before we proceed, let’s answer the fundamental question – what is an IP address?

What Is An IP Address?

An IP (Internet Protocol) address is a series of numbers assigned to computers in a network. The internet is a network and as you browse the web, you do so with your device’s IP address. The major function of IP addresses is that they enable web servers to communicate with browsing devices.

What Information Can I Get From an IP Address Lookup?

When an IP address is checked, there are two main details you can get. The first is the browsing location of the device. This doesn’t imply to the smallest detail, i.e Street, home, etc. but rather cities, and countries. The second is the internet service provider (ISP) being used by the device.

How To Get IP Address In JavaScript

JavaScript is a client-side scripting language that does not store the client IP address. However, you can create http requests with JavaScript to retrieve the client IP address.

Thankfully, there are third-party API services that you can use to get an IP address in JavaScript.

Note: You can save the code snippet in .html file format, while the JavaScript function generates the IP address in the output.

  • Ipify

Ipify has a public IP address API. It works for identifying IPV4 and IPV6. In fact, it is an open-source API that you can contribute to on GitHub. You can discover IP addresses by simply visiting – https://api.ipify.org/?format=json. Lastly, Ipify collects no logs.

Program:

<html xmlns=”http://www.w3.org/1999/xhtml”><head><title></title><script type=”text/javascript”>window.onload = function () {var script = document.createElement(“script”);script.type = “text/javascript”;script.src = “https://api.ipify.org?format=jsonp&callback=DisplayIP”;document.getElementsByTagName(“head”)[0].appendChild(script);};function DisplayIP(response) {document.getElementById(“ipaddress”).innerHTML = “Your IP Address is ” + response.ip;}</script></head><body><form><span id = “ipaddress”></span></form></body></html>
  • Cloudflare

Cloudflare is a popular web security platform. The site has several resources and solutions for developers and programmers including a CDN CGI tracing tool. This tool can be accessed at https://www.cloudflare.com/cdn-cgi/trace, and it allows you to get your IP address and other details as well.

Program:

$.get('https://www.cloudflare.com/cdn-cgi/trace', function(data) {
    console.log(data)
})
  • GeoBytes

GeoBytes is one of the oldest providers of IP Geolocation solutions. You can use the Get City Details API, a free version of the GeoBytes IP Locator API to get an IP address in JavaScript. You can see a sample of the generated results when the API is used here – http://gd.geobytes.com/GetCityDetails.

Program:

$.getJSON('http://gd.geobytes.com/GetCityDetails?callback=?', function(data) {
  console.log(JSON.stringify(data, null, 2));
});
  • GeoIPLookup

GeoIPLookup gets IP addresses with detailed information. Aside from the location and internet service provider, it also delivers ZIP/Postal codes, coordinates, ASN, and connection type. The GeoLookup API can be used via JSON or JSONP, you can get it here – https://json.geoiplookup.io/api.

Program:

$.getJSON('https://json.geoiplookup.io/api?callback=?', function(data) {
  console.log(JSON.stringify(data, null, 2));
});
  • GeoPlugin

The GeoPlugin API detects requests from browsers and identifies their location from their IP address. You can test the results of GeoPlugin API for Javascript here – http://www.geoplugin.net/json.gp.

Program:

$.getJSON('http://www.geoplugin.net/json.gp?jsoncallback=?', function(data) {
  console.log(JSON.stringify(data, null, 2));
});

Other third-party APIs you can use include:

How To Run JavaScript Codes In Notepad++

Notepad++ is a source code editor for Microsoft Windows computers. For beginners, Notepad++ is the go-to editor for writing JavaScript codes and programs. But, they do find it difficult to run their program after completing it.

If you’ve written your JavaScript code to get an IP address in Notepad++, follow these steps to run it:

  • Open Notepad++ and type in your JavaScript code

  • Enclose the code using <script> and </script> tags. The <script> tag is the default tag to contain scripting statements like JavaScript codes.

  • Click on File and then Save As

  • Set Save as type to All Files and save the file with a .html extension

  • Double click on the file where it is saved and it will open and run in your default browser. You can right-click and select Run too.

You can also run JavaScript codes in browser consoles so next, we look at how to run JavaScript codes on two of the most popular web browsers – Google Chrome and Mozilla Firefox.

How To Run JavaScript Codes In Google Chrome

Follow the steps below to run JavaScript Codes in Google Chrome:

  • Load Google Chrome browser and visit any website or web page. If there’s a specific website where you want the command to run, visit it instead.

  • Navigate to the Chrome menu by clicking on the three dots (located at the top right corner). Go to More Tools and then Developer Tools. Alternatively, use the shortcut CTRL + SHIFT + I as well.

  • Select Console from the menu options located at the top. You can use the shortcut CTRL + SHIFT + J from the web page to get here too.

  • If there’s any data in the console, clear it by using CTRL + I

  • Enter your JavaScript code and press enter to execute

  • The result will be displayed in a pop up at the top of the page

  • Click OK to dismiss when you are done

How To Run JavaScript Codes In Mozilla Firefox

Follow the steps below to run JavaScript Codes in Mozilla Firefox:

  • Load your browser and visit any website or web page. If there’s a specific website where you want the command to run, visit it instead

  • Click on the browser menu and select Web Developer, and then Web Console. You can use the shortcut CTRL + SHIFT + K as well.

  • If there’s any data in the console, clear it by

  • Enter your JavaScript code and press enter to execute

  • The result will be displayed on your screen

What Is The Difference Between Proxy And IP Address?

A proxy is a server that acts as a gateway between the web and an internet-enabled device. It is like an application that sends and seeks web resources from the internet on behalf of the browsing computers which are the clients. An IP address, as defined earlier is a series of numbers that are assigned to a computer in a network.

Nevertheless, an IP address and Proxy are not the same. While an IP address represents your computer, a proxy server is another computer that sends requests on your behalf. Likewise, the Proxy server has its unique IP address for accessing the web.

Proxy servers are mainly used to stay anonymous online or bypass geo-restrictions. If you access the web via a proxy server, the IP address of the proxy server will be picked on the web and not the actual one. Hence, you can use proxy servers with foreign IP addresses to hide your identity and bypass geo-restrictions.

Rounding Up

JavaScript can be used to carry out several functions; it is just as amazing as to what you can do with this programming language. In this post, we looked at how you can get an IP address in JavaScript by using any of the third-party API services.

Interestingly, the third-party APIs listed above are all free to use, they won’t cost you a thing. Also, we looked at how you can run JavaScript codes in Notepad++ and popular web browsers like Mozilla Firefox and Google Chrome.

If you are looking for a reliable proxy network provider, ProxyRack can provide you with over 2 million unique IP addresses for obfuscating your web traffic.

Find the perfect Proxy Product.

Proxyrack offers a multiple options to suit most use cases, if you are unsure our 3 Day Trial allows you to test them all.
Security

Residential proxies

Never get blocked, choose your location
View all option available
Vault

Datacenter proxies

Super fast and reliable
View all option available
Try

3 Day Trial

Test all products to find the best fit
View all option available

Related articles

Get Started by signing up for a Proxy Product