Hello, there is such a task .. in contacts for visitors to the country of Russia to show one block of contacts, for visitors from Ukraine another, for all the other two blocks simultaneously

Attacked the Internet on http://dev.maxmind.com/geoip/geoip2/javascript/

And I wrote this code:

<head> <meta charset="UTF-8"> <title>Test GeoIP</title> <script src="http://js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script> <style type="text/css"> .geoip { display: none; border: 1px solid #036; padding: 10px; margin: 10px; } p { font-size: 0.8em; text-align: center; } </style> <script type="text/javascript"> function init() { var id, country = geoip2.country(); switch (country) { case 'RU': id = 'country-ru'; break; case 'UA': id = 'country-ua'; break; default: id = 'country-other'; } document.getElementById(id).style.display = 'block'; } </script> </head> <body onload="init();"> <div class="geoip" id="country-ru"> <h1>Блок для пользователей из России</h1> </div> <div class="geoip" id="country-ua"> <h1>Блок для пользователей из Украины</h1> </div> <div class="geoip" id="country-other"> <h1>Блок для пользователей из неопределенной страны</h1> </div> </body> 

But something does not work out at all. It turns out that I cannot pull out the "iso_code" myself ...

Maybe someone will offer some other solution?

    3 answers 3

    so why are you writing

     switch (country) { 

    if you have to write

     switch (country.iso_code) { 

    ?

    • I tried, there is an error "Cannot read property 'iso_code' of undefined" .. I now think MB is a feature of the free version .. - D.kpaqpa
    • Well, it means that your function does not work - it returns undefined and not the country object. The thing is that this script should be connected to the body, and not to the head - transfer <script src = " js.maxmind.com/js/apis/geoip2/ v2.1 / geoip2.js "type =" text / javascript "> </ script> to the end before </ body> and it will work - Eugene Bartosh
    • sorry .. but something is no change, probably try some other option / service - D.kpaqpa
    • and what is this for; after src ...? the first steps? I read a book some hope? )) Do you even see in the console what happens when you boot? Script errors, downloads and stuff? - Eugene Bartosh
    • there is probably another problem with this - onload = "init ();" ... trying to use the document data before it is ready. better connect jquery and make a call from $ (document) .ready () - learn.jquery.com/using-jquery-core/document-ready - Eugene Bartosh

    just check Ukraine - uk. http://www.localeplanet.com/icu/uk-UA/ ISO 639

      Try this:

       function init() { geoip2.country(function(result) { var id; switch (result.country.iso_code) { case 'RU': id = 'country-ru'; break; case 'UA': id = 'country-ua'; break; default: id = 'country-other'; } document.getElementById(id).style.display = 'block'; }, function(error) { // вывод ошибок }); } 
      • Thank you, your version has earned) and I have already implemented it via yandex.geolocation, it was easier there), but it seems there is a longer load, I will test and choose the option - D.kpaqpa