How can I geo target ads/banners with javascript?

帅比萌擦擦* 提交于 2019-12-03 20:26:03

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 ? 

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!