Hello.

I try to determine by means of the embedded code on the site some data of a person, for example, where he is (city, country).

function geopotision(){ var script = document.createElement('script'); script.src = "http://api-maps.yandex.ru/2.0/?load=package.full&lang=ru-RU"; document.getElementsByTagName("head")[0].appendChild(script); var map; var city, country; ymaps.ready(init); function init () { var geolocation = ymaps.geolocation; city = geolocation.city; country = geolocation.country; // console.log(city); // console.log(country); } } 

in the browser panel says: Uncaught ReferenceError: ymaps is not defined

What do you tell me?

    1 answer 1

    Your script does not have time to load. Try this

     function geopotision(){ var script = document.createElement('script'); script.src = "http://api-maps.yandex.ru/2.0/?load=package.full&lang=ru-RU"; document.getElementsByTagName("head")[0].appendChild(script); var map; var city, country; function init () { if (typeof ymaps === 'undefined' || typeof ymaps.geolocation === 'undefined') { setTimeout(init, 100); return; } var geolocation = ymaps.geolocation; city = geolocation.city; country = geolocation.country; console.log(city); console.log(country); } init(); } 
    • Thanks for the decision. - Alexander Sizintsev