问题
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)....
回答1:
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>
来源:https://stackoverflow.com/questions/10996961/how-can-i-geo-target-ads-banners-with-javascript