0

I'm trying to hide an element based on an IP-address.

With this script I can write out the IP-adress in the DOM:

<script type="text/javascript" src="https://l2.io/ip.js?var=userip"></script>
<script type="text/javascript">document.write("Your IP is :", userip)</script>

How Do I target this to remove the element below based on a specific IP-address?

4
  • i think this: if(userip == "127.0.0.1"){//remove your elements} should work. Commented Nov 17, 2016 at 10:27
  • Do note that this system is entirely unreliable for any real-world uses. IP can very easily be spoofed, client-based protection can also easily be bypassed. Commented Nov 17, 2016 at 10:33
  • 1
    @vlaz good point, but differentiating ip addresses may improve UX by delivering different contents on the upside. Commented Nov 17, 2016 at 10:40
  • @DanielCheung also not a good idea to do it in JS client-side. Doing either on the server is way easier - first of all, you don't have to use a workaround to find the IP. That one is a big give away that doing this client-side is probably not what you want. Commented Nov 17, 2016 at 11:06

1 Answer 1

1

just use document ready and check the ip. IF it matches the condition do your stuff

    var ip=['123.123.123','111.111.11']
     $(document).ready(function () {
if(ip.indexOf(userip)!=-1)
{ your  code here
}
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.