3

Do you know of any way to do it? real example...?

I am looking for a free service like maxmind or others (I really don't care what) and I would like to have a different ad for US visitors.

Thanks a lot!

2astalavista: Your example works fine. This is what I did and it's still not working.

<html>
<head>
<title>Geo Test</title>
<script type='text/javascript' src='http://www.101greatgoals.com/wp-includes/js/jquery/jquery.js?ver=1.7.1'></script>
<script>
$(document).ready( function() {
    $.getJSON( "http://smart-ip.net/geoip-json?callback=?",
        function(data){            
            console.log(data);
            var c = data.countryCode;
            if(c=="US" || c=="US" ){
                document.getElementById('ddd').innerHTML = 'US'; } else {
                    document.getElementById('ddd').innerHTML = 'Not US';}
            /*
            this service needs ip
            var ip = data.host;
            alert(ip);
            $.getJSON( "http://freegeoip.net/json/"+ip,
                function(data){
                    console.log(data);
                }
            );*/
        }
    );

});?
</script>
</head>
<body>
<div id="ddd"></div>
</body>
</html>

Don't know if it's the server (amazon) or the CDN (cotendo)....

JJJ
  • 32,902
  • 20
  • 89
  • 102
user1341839
  • 75
  • 1
  • 13

1 Answers1

3

I found these: http://freegeoip.net/static/index.html and http://smart-ip.net

example:

$.getJSON( "http://smart-ip.net/geoip-json?callback=?",
    function(data){
        var c = data.countryCode;
        if(c=="US" || c=="USA" )
            alert("American visitor!");else
                alert("Not american visitor! ("+c+")");
    }
);

Why is your code not working?

1) You should take care of the error messages:

Uncaught SyntaxError: Unexpected token ? 

enter image description here

remove ?

2) error again:

Uncaught TypeError: Property '$' of object [object Window] is not a function 

this means jquery does not work for some reason.

fix include link according to this!

now it works :)

<html>
<head>
<title>Geo Test</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-latest.min.js'></script>
<script>
$(document).ready( function() {
    $.getJSON( "http://smart-ip.net/geoip-json?callback=?",
        function(data){            
            console.log(data);
            var c = data.countryCode;
            if(c=="US" || c=="US" ){
                document.getElementById('ddd').innerHTML = 'US'; } else {
                    document.getElementById('ddd').innerHTML = 'Not US';}
        }
    );

});
</script>
</head>
<body>
<div id="ddd"></div>
</body>
</html>
Community
  • 1
  • 1
  • Tried to do this: not working :( – user1341839 Jun 14 '12 at 08:04
  • 1
    it is working, see the example I have linked (http://jsfiddle.net/UHgu4/5/). You need jquery to run this code. –  Jun 14 '12 at 08:38
  • Tried your example and it works great. Don't know why it's not working on my site/server - I'm using Amazon EC2 and Cotendo CDN. @2astalavista – user1341839 Jun 17 '12 at 09:51
  • This thing - @2astalavista, doesn't seem to be working. Am I right? – user1341839 Jun 17 '12 at 12:11