General developer forum

[Moodle 3.2] How do I include a third party js non-AMD js file in my page?

 
Picture of Daniel Poggenpohl
[Moodle 3.2] How do I include a third party js non-AMD js file in my page?
 
Hi,

I have a third party JS module (http://www.numbox.org/) which I want to use on my page. I've only found documentation about how to include third party AMD modules. I either don't understand it correctly or haven't found the right place to look for this information.

Can you help?

What I found:
https://docs.moodle.org/dev/Javascript_Modules#Including_an_external_javascript.2Fjquery_library
 
Average of ratings: Useful (1)
Picture of Conn Warwicker
Re: [Moodle 3.2] How do I include a third party js non-AMD js file in my page?
Group Plugin developers

I don't know if it's the "correct" way or not, but you can just tell it to include it with all the other javascript:

$PAGE->requires->js( '/path/to/my/file.js' );

 
Average of ratings: -
Picture of Daniel Poggenpohl
Re: [Moodle 3.2] How do I include a third party js non-AMD js file in my page?
 

Thanks for the hint, but just including it via require->js doesn't work anymore.

The Plugin depends on jQuery being a global variable to extend it with functions.

Moodle does a $.noConflict() call to remove all global references to jQuery ($, jQuery) so that only AMD modules get references to jQuery.

I would edit the original post to reflect this reality, but I can't anymore...

 
Average of ratings: -
Picture of tim st.clair
Re: [Moodle 3.2] How do I include a third party js non-AMD js file in my page?
Group Plugin developers

Can you put it back at the window level by first using the amd loader to load jquery (haven't tried)

$PAGE->requires->js_call_amd('myblock/stub', 'init');

then having your stub

define(['jquery'], function($) {

var init = function () {

window.$ = $;

}
})

then calling your plugin via:

$PAGE->requires->js('plugin-path');

 
Average of ratings: -