javascript - How to show or hide element based on Geo ip -


i have script give me geo location, "ca" or "us". want show specific in html base on user location. suggestions?

html:

<div id="ip">loading...</div> <div id="country_code"></div> <div id="country_code">hello canada</div> <div id="country_code">hello usa</div>   <script>     $.get("http://freegeoip.net/json/", function (response) {         $("#ip").html("ip: " + response.ip);         $("#country_code").html(response.country_code);     }, "jsonp");     document.getelementbyid("us").style.display = "block";     document.getelementbyid("ca").style.display = "block"; </script> 

css:

#us { text-align: left; color: blue; display:none;}  #ca { text-align: left; color: blue; display:none;}  

jsfiddle

as commented, simple if condition work.

$.get("http://freegeoip.net/json/", function (response) {      $("#ip").html("ip: " + response.ip);      $("#country_code").html(response.country_code);      if(response.country_code=='ca'||response.country_code=='us'){      	document.getelementbyid(response.country_code).style.display = "block";  	}  }, "jsonp");
#us { text-align: left; color: blue; display:none;}   #ca { text-align: left; color: blue; display:none;} 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id="ip">loading...</div>  <div id="country_code"></div>  <div id="ca">the ca</div>  <div id="us">the us</div>

if have questions, leave comment below , possible.

i hope helps. happy coding!


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -