U
U
uzi_no_uzi2020-03-28 16:04:39
JavaScript
uzi_no_uzi, 2020-03-28 16:04:39

How to embed the script on the page?

I'm trying to change yandex cards.

There is a mapInit() function; , which simply initializes the map when the document is ready.

Next, here is the code:

function createMapLang() {
            let lang = localStorage.getItem('lang');

            var newMap = `<script data-change='true' src="https://api-maps.yandex.ru/2.1/?apikey=f6857b56-4852-45e5-8221-f84bb63b1169&lang=${lang}"></script>`

            $('body').append(newMap);
                mapInit();
            
            

        }

        createMapLang();


        

        $('.lang-list__link').each(function() {
            $(this).click(function() {
                let lang = $(this).attr('data-lang');
                localStorage.setItem('lang', lang);

                window.location.reload();
            })

        })


When changing the language, the language code is entered into the local storage and the page is reloaded, when the page is reloaded, createMapLang is called, which generates a new script and inserts it into the page.

But in the console I get this error:

ReferenceError: ymaps is not defined


I think this may be due to the fact that I am not adding the script at the right time, or after adding the script it has not yet had time to load, what could be the reasons?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question