I want to add js code to a page [closed]

1

I am trying to add javascript element to my web page, but the thing is I do not want to add it in html , I want to add it from javascript / em>.

I had tried something like document.write ,  but this replaces the whole page so what I want is to add this <script https://maps.googleapis.com/maps/api/js?key=" + API_KEY/>

    
asked by DDH 05.10.2017 в 15:06
source

1 answer

4

If you use jQuery you could do it like this:

$.getScript('https://maps.googleapis.com/maps/api/js?key=API_KEY');

In this case you can call a callback function when it ends:

$.getScript('https://maps.googleapis.com/maps/api/js?key=API_KEY', function() {
    // hacer algo cuando se ha cargado la librería
});

With JavaScript you could do it like this:

var scriptTag = document.createElement('script');
scriptTag.src = 'https://maps.googleapis.com/maps/api/js?key=API_KEY';
document.body.appendChild(scriptTag);
    
answered by 05.10.2017 / 15:27
source