jquery .getscript() callback when script is fully loaded and executed

jquery load script tag
jquery getscript example
jquery getscript once
jquery getscript cors
$.getscript is not a function
dynamically load javascript library
how to reload js file using jquery
javascript load script dynamically

As from the jquery api page http://api.jquery.com/jQuery.getScript/

Success Callback

The callback is fired once the script has been loaded but not necessarily executed.

$.getScript("test.js", function() {
    foo();
});

if the foo() function depend on test.js, it cannot be execute successfully.

I saw similar thing with google map api but in that case you can specify the callback function in the ajax script url. But in general is there an obvious way to wait until the ajax script executed to do the callback?

I know it is an old question but i think both answers containing "done" are not explained by their owners and they are in fact the best answers.

The most up-voted answer calls for using "async:false" which will in turn create a sync call which is not optimal. on the other hand promises and promise handlers were introduced to jquery since version 1.5 (maybe earlier?) in this case we are trying to load a script asynchronously and wait for it to finish running.

The callback by definition getScript documentation

The callback is fired once the script has been loaded but not necessarily executed.

what you need to look for instead is how promises act. In this case as you are looking for a script to load asynchronously you can use either "then" or "done" have a look at this thread which explains the difference nicely.

Basically done will be called only when a promise is resolved. in our case when the script was loaded successfully and finished running. So basically in your code it would mean:

$.getScript("test.js", function() {
    foo();
});

should be changed to:

$.getScript("test.js").done(function(script, textStatus) {
    console.log("finished loading and running test.js. with a status of" + textStatus);
});

ajax, Success Callback. The callback is fired once the script has been loaded but not necessarily executed. $.getScript("test.js", function() { foo(); });. if the foo() function​  The callback is fired once the script has been loaded but not necessarily executed. $.getScript("test.js", function() { foo(); }); if the foo() function depend on test.js, it cannot be execute successfully. I saw similar thing with google map api but in that case you can specify the callback function in the ajax script url. But in general is there an obvious way to wait until the ajax script executed to do the callback?

You can use ajax

jQuery.ajax({
        async:false,
        type:'GET',
        url:script,
        data:null,
        success:callback,
        dataType:'script'
    });

Async is false, because you want to load and execute the script, after that in success callback you can call your function foo()

jQuery.getScript(), Success Callback. The callback is fired once the script has been loaded but not necessarily executed. Scripts are included and run by referencing the file name:  The script is executed in the global context, so it can refer to other variables and use jQuery functions. Included scripts can have some impact on the current page. Success Callback The callback is fired once the script has been loaded but not necessarily executed. Scripts are included and run by referencing the file name:

$.getScript(...) + setInterval(...) worked for me:
$.getScript('https://www.google.com/recaptcha/api.js')
  .done(function() {
    var setIntervalID = setInterval(function() {
      if (window.grecaptcha) {
        clearInterval(setIntervalID);
        console.log(window.grecaptcha);
        letsWorkWithWindowDotGreptcha();
      };
    }, 300);
  });


  function letsWorkWithWindowDotGreptcha() {
    // window.greptcha is available here....
  }

Once the variable we are looking for is defined (in my case window.grecaptcha, i can call a closure function which can be abstracted out.

Good Luck...

jQuery getScript() Method, The callback is fired once the script has been loaded but not necessarily executed. $.getScript("test.js", function() { foo(); });. if the foo() function  l'ideia est que vous chargez un script donné qui exposera une variable (la fonction foo dans votre exemple de question) qui n'existe toujours pas, donc après que le script est chargé par getScript vous vérifiez si cette variable existe, sinon vous créez un intervalle qui vérifiera en permanence si la variable est disponible, et seulement

$.getScript( "ajaxFile/myjs.js" )
.done(function( s, Status ) {
    console.warn( Status );
})
.fail(function( jqxhr, settings, exception ) {
    console.warn( "Something went wrong"+exception );
});

Script to load other .js scripts, The getScript() method is used to get and execute a JavaScript using an AJAX HTTP GET request. Syntax. $(selector).getScript(url,success(response,status))  The callback is fired once the script has been loaded but not necessarily executed. $.getScript("test.js", function() { foo(); }); if the foo() function depend on test.js, it cannot be execute successfully. I saw similar thing with google map api but in that case you can specify the callback function in the ajax script url.

I'm afraid the solution requires polling for the dependency. Alternatively the script needs to be wrapped in a pre-defined callback like AMD.

How to load JavaScript dynamically with jQuery, window.load_scripts = function () { var callbacks = { jqueryCallback: function (​script_path) { $. jQuery's getScript doesn't add a script to the page like your vanilla uses eval to execute the code, which means any safe mode scripts will not Don't forget to add your onload callback so scripts that use this to  The getScript docs say about the success callback that: "The callback is fired once the script has been loaded but not necessarily executed." But in my testing that doesn't seem to be true. For a host page with: var startTime = new Date(

Struggling With Load Order of jQuery Scripts - JavaScript, You can also provide a callback function that will be executed immediately getScript("my-library-core.min.js", function() { alert("Script loaded and If you're new to jQuery and appreciate simple tips like this, consider purchasing the full-​color  The advantage of using $.getScript is that it loads the content on run time, which is far better than including <script> tag in your head section. jQuery getScript load a JavaScript file from the server using a GET HTTP request, then execute it. The syntax of $.getScript is,

How to use jQuery.getScript() method to load external js files?, <script src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript" match : function() { loadJS('http://cdn.jquerytools.org/1.2.7/full/jquery.tools.min.js'); getScript() function to load jQuery Tools, passing an init function for the slider as a callback: Neither the success nor error callback is getting executed. Note that although the DOM always becomes ready before the page is fully loaded, it is usually not safe to attach a load event listener in code executed during a .ready() handler. For example, scripts can be loaded dynamically long after the page has loaded using methods such as $.getScript().

Thinking Async, getScript( url, [callback] ) method loads and executes a JavaScript file using an a function to be executed whenever the data is loaded successfully. <head> <​script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/  The obvious use case for jQuery.getScript is lazy loading of a plugin and using it once loaded: jQuery.getScript("jquery.cookie.js") .done(function() { jQuery.cookie("cookie_name", "value", { expires: 7 }); }); If you need to do more advanced stuff like loading multiple scripts and different types of file types (text files, images, css files

Comments
  • did you get an error message?
  • the function is undefined.
  • also "undefined is not a function "
  • if you're using chrome or firefox; does it echo to the console if you put console.log('xxxx'); into the callback function rather than foo();
  • Actually, in most cases the callback does fire after the script is executed - see this question and the issue raised on jQuery's GitHub. You need to figure out what's unique about your situation. Also @richb01, your site is down.
  • The issue is that success, .then, and .done, etc, are all called when the HTTP call completes, not when the fetched script is available to be executed. The problem with async issues is that they work for some people and not others, so you the results are dominated by "works for me, clearly you just don't understand promises" answers, which is what we have here. Well, this didn't work for me. :)
  • It still indicate that the prerequisite function in the script is not executed yet :(
  • I use this code and works for me, i had the same problem that you have. but was because get script has a callback function that is executed if the request succeeds, but the code is not available yet.
  • Thanks a lot anyway, I also saw this on documentation "As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success()." Not sure if it is related.
  • Does not solve the problem. It makes the HTTP call async, but the script is still not loaded inside success/.then.
  • I would say this is the best solution possible while I don't like dealing with setInterval(...) in my script.
  • Please try and explain your code a little. Stack Overflow is not a site to get "just the solution", the purpose is also to build knowledge. That's why code-only answers rarely deserve upvotes in my opinion.
  • Callback methods are deprecated in jQuery, Deferred handlers are the tested and supported method for attaching behavior to async loads. That said, I'm not sure they guarantee that the script will be executed by the time they're called.
  • Please try and explain your code a little. Stack Overflow is not a site to get "just the solution", the purpose is also to build knowledge.
  • I think you've missed the point. The callback needs to use something defined in the script you just loaded.