Search code examples
jquery.when

Why does my jQuery .done function not fire after .when code?


I tried to load some scripts with jQuery, and after the download is finished I want to execute some code.

I expected to get alert(1) called but nothing happens. The scripts are correctly downloaded (checked in console).

$.when(
    $.getScript ("/framework/static/libs/ckeditor/ckeditor/ckeditor.js"),
    $.getScript ("/framework/static/libs/ckeditor/ckeditor/config.js"),
    $.getScript ("/framework/static/libs/ckeditor/ckeditor/skins/bootstrapck/editor_gecko.css"),
    $.getScript ("/framework/static/libs/ckeditor/ckeditor/lang/de.js"),
    $.getScript ("/framework/static/libs/ckeditor/ckeditor/adapters/jquery.js"),
    $.Deferred(function( deferred ){$( deferred.resolve );})
).done(function() { 
        alert (1);
});

Solution

  • I hope the issue is in the below line.

    $.getScript ("/framework/static/libs/ckeditor/ckeditor/skins/bootstrapck/editor_gecko.css")
    

    As you can find in the documentation that it loads a JavaScript file from the server using a GET HTTP request, then executes it.

    For css files you have to may use the code below.

    window.getStyle = function(file) {
      var top = $('head > link[rel=stylesheet]').length ? $('head > link[rel=stylesheet]:last') : $('head > *:last');
      top.after('<link rel="stylesheet" type="text/css" href="' + file + '">');
    };
    
    $.when(
      $.getScript("/framework/static/libs/ckeditor/ckeditor/ckeditor.js"),
      $.getScript("/framework/static/libs/ckeditor/ckeditor/config.js"), window.getStyle("/framework/static/libs/ckeditor/ckeditor/skins/bootstrapck/editor_gecko.css"),
      $.getScript("/framework/static/libs/ckeditor/ckeditor/lang/de.js"),
      $.getScript("/framework/static/libs/ckeditor/ckeditor/adapters/jquery.js"),
      $.Deferred(function(deferred) {
        $(deferred.resolve);
      })
    ).done(function() {
      alert(1);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>