Download external js file using jQuery

He looked at him everywhere and found the answer, but lost. Does anyone know how to load an external .js file from another js file?

main_lobj1.onreadystatechange = function(){ if (main_lobj1.readyState == 4) {if (main_lobj1.status == 200) { document.getElementById("middleDiv_m").innerHTML=main_lobj1.responseText; jQuery.getScript('jquery/tabs.js') } } 

innerHTML overlays text and response text. The problem is that JS is not working in this document. jQuery.getScript should load this external js file, but it doesn't

+4
source share
3 answers

So you can do this to load an external js file into jQuery

 $.ajax({ type: "GET", url: "test.js", dataType: "script" }); 
+5
source

For this reason, there is a $ function in jquery . getScript () . You can use it just like that:

 $.getScript("test.js", function( data, textStatus, jqxhr ) { // this is your callback. }); 
+4
source

If you call an external script that you want to host on your script, you might want to note that jquery based on ajax script is asynchronous by defealt.

Invoking an external script asynchronously will cause the rest of the rest to be completed before loading the external script.

Today I ran into the same problem that was easily resolved by making a small reference to a message from Sam Arul Raj:

 $.ajax({ type: "GET", url: "test.js", dataType: "script", async: false }); 
+2
source

Source: https://habr.com/ru/post/1398335/


All Articles