On the index.html page, the script <script src="scriptjs.js" type="text/javascript"></script> is connected, I am interested in the question how to connect another script within this scriptjs.js , loaded by reference - <script src="http://jquery.com/jquery.js" type="text/javascript"></script> and for this script to run correctly on the page.

2 answers 2

This can be achieved by manually manipulating the DOM nodes. Add these lines in your first script.

 var s = document.createElement('script'); s.src = '/ΠΏΡƒΡ‚ΡŒ/ΠΊ/ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌΠΎΠΌΡƒ/ΡΡ†Π΅Π½Π°Ρ€ΠΈΡŽ'; document.head.appendChild(s); 

Remember, the download will be asynchronous. To track the moment of loading, use the onLoad event of the newly created element, it is necessary to assign values ​​to it before inserting it into the page. Inside, a zero timer is set to ensure that our code runs after loading the inserted script.

 s.onload = function() { // Π‘ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠ° Π·Π°Π³Ρ€ΡƒΠΆΠ΅Π½Π°, Π½ΠΎ Π΅Ρ‰Ρ‘ Π½Π΅ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΠΎΠ²Π°Π½Π°, Π΅Ρ‘ ΠΊΠΎΠ΄ ΠΏΠΎΠΊΠ° Π½Π΅ Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½, // поэтому ΠΏΠ»Π°Π½ΠΈΡ€ΡƒΠ΅ΠΌ запуск нашСго ΠΊΠΎΠ΄Π° Π² ΡΠ»Π΅Π΄ΡƒΡŽΡ‰Π΅ΠΌ Ρ‚ΠΈΠΊΠ΅. setTimeout(function() { // Π’ΡƒΡ‚ ΠΊΠΎΠ΄, ΠΎΠΏΠΈΡ€Π°ΡŽΡ‰ΠΈΠΉΡΡ Π½Π° использованиС ΠΈΠΌΠΏΠΎΡ€Ρ‚ΠΈΡ€ΡƒΠ΅ΠΌΠΎΠΉ Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠΈ }, 0); }; 

    Another approach is to use the document.write function. This option is much simpler, but less flexible.

     document.write('<script src="/ΠΏΡƒΡ‚ΡŒ/ΠΊ/ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌΠΎΠΌΡƒ/ΡΡ†Π΅Π½Π°Ρ€ΠΈΡŽ"></script>'); 

    No download processing is needed - this function is executed in synchronous mode. Right after you can write your code that relies on the library. But remember, this method slows down the loading of the page, in addition, it can only be used in synchronously connected scripts (without the async attribute).