On a specific event, I need to load an additional javascript file and, after downloading and executing it, perform some kind of action. According to the task, I cannot enter any code in the loaded script (maybe a third-party developer or from a CDN, for example)

The first part of the task I do as follows

var mscript = document.createElement("SCRIPT"); var head = document.getElementsByTagName( "head" )[ 0 ]; mscript.type = "text/javascript"; mscript.src = 'http://somesite.ru/script.js'; head.appendChild(mscript); 

Is there any event that allows you to perform an action after loading and executing the script. (I am currently using a timeout). If necessary, you can use jquery for the solution.

  • 2
    onload at the script element - Grundy
  • @Grundy, thanks. I did not notice the elephant .... Why a comment, but not an answer? I would mention it .... - Vorobev Alexander
  • you can write it yourself, maybe there is even a duplicate - Grundy

1 answer 1

Problem solved by comment Grundy. To solve this problem, it is necessary to determine onload for the script element.

 var mscript = document.createElement("SCRIPT"); var head = document.getElementsByTagName( "head" )[ 0 ]; mscript.type = "text/javascript"; mscript.src = 'http://somesite.ru/script.js'; mscript.onload = function(){alert('Ok');}; head.appendChild(mscript);