Search code examples
javascriptip-address

A way of hiding a div for a set of IP addresses - using javascript


There are ways of recognising IP addresses from certain countries so I can use $("#myDiv").hide(); to not show a div when these countries occur.

However, I want to recognise IP addresses from a single company. The company can give me the format of their IP address.

What do I need in javascript? What function tells me their IP in javascript? What part of their IP will pinpoint the company?

It is nothing important. I just want to hide adverts for certain companies using my website. I want to use javascript, but am willing to call a php script on my web server if necessary using ajax which will then be processed in javascript in my webpage.

What can I do?


Solution

  • You can't get a visitor's IP address from javascript in the browser client. However, you can get it from the HTTP_X_FORWARDED_FOR header which is sent to your server and then load that into the html/js sent to the visitor browser.. Alternatively, you can send a query to https://jsonip.com from your javascript to get the visitor's IP.