Load Javascript after the preloadedJs

Forums: 

Hello,

Iḿ trying to develope a h5p content, but I need to add javascript after the dom has loaded because the js is a complete react app and needs the div container created from the "preloadedJs".

I tried to load the js with jquery getscript in the "C.prototype.attach" method with a relativ path. A "test.js" file in the same folder that contains the file with the "C.prototype.attach" function is cannot be found.

Does anybody know here, how I can load custom javascript after the dom load has finished?

This is my questionnaire.js file:

var H5P = H5P || {};

H5P.Questionnaire = (function ($) {
  /**
   * Constructor function.
   */
  function C(options, id) {
    // Extend defaults with provided options
    this.options = $.extend(true, {}, {
      language: 'en'
    }, options);
    // Keep provided id.
    this.id = id;
  };

  /**
   * Attach function called by H5P framework to insert H5P content into
   * page
   *
   * @param {jQuery} $container
   */
  C.prototype.attach = function ($container) {

    // Set class on container to identify it as a greeting card
    // container.  Allows for styling later.
    $container.addClass("h5p-questionnaire");
    // Add image if provided.
    // if (this.options.image && this.options.image.path) {
    //   $container.append('<img class="greeting-image" src="' + H5P.getPath(this.options.image.path, this.id) + '">');
    // }
    // Add greeting text.
    $container.append('<div class="questionnaire" id="learningstyle"></div>');
    $(document).ready(function () {
      $.getScript("test.js")
        .done(function (script, textStatus) {
          console.log(textStatus);
        })
        .fail(function (jqxhr, settings, exception) {
          console.log('getscript failed');
          console.log('jqxhr');
          console.log(jqxhr);
          console.log('settings');
          console.log(settings);
          console.log('exception');
          console.log(exception);
        });
    });
  };

  return C;
})(H5P.jQuery);

 

Thanx very much.

frischi

 

BV52's picture

Hi Frischi,

I'm sorry that you have to post several times because you are not seeing them immediately. The reason for this is to avoid spam all new threads are not published automatically but don't worry we routinely check these and publish them asap.

-BV52