且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用javascript定位广告/横幅广告?

更新时间:2022-10-21 23:27:28

我发现了这些:修复包含链接!



现在它有效:)

 < 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=?,
函数(数据) ){
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>


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)....

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>