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.
- 2association: stackoverflow.com/q/4634644 - mymedia
2 answers
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).