Issue loading external script with requirejs

Issue loading external script with requirejs

by Matt Sawatzky -
Number of replies: 4

Hey guys,

I am running Moodle 3.6.3 and really struggling with trying to add an external javascript link to my site.

Adding like normal with a script tag causes other javascript to break with errors in the console like:


According to RequireJS documentation, I tried this loading it through Require js with a script tag like:

<script data-main="https://...js" src="/lib/requirejs/require.min.js"></script>

and got different errors with javascript still broken:

error_1

I am running out of options here but this is a must get done one way or another. I'm not well versed in the javascript modules, but everything I can find, it seems like the script needs to be run locally, and that is not possible with the specific javascript.


Any help would be much appreciated! 

Thanks,


Matt

Average of ratings: -
In reply to Matt Sawatzky

Re: Issue loading external script with requirejs

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Have you looked at the 'js' method in the 'page_requirements_manager' class?  As in $PAGE->requires->js - which should not really be used, but might be quick to see if it works and solves the issue without creating a module (AMD or YUI).  If it does not then the external JS is probably breaking things and would need to be in a form whereby it defines itself as an AMD module to be included.  Initially start here: https://docs.moodle.org/dev/Javascript_Modules.

In reply to Gareth J Barnard

Re: Issue loading external script with requirejs

by Matt Sawatzky -

It looks like you can't use the 'js' method in the 'page_requirements_manager' class with an external URL.


I've read over the documentation you linked and couldn't find any examples of how to add an external URL using the javascript modules. Do you possibly have any further information on how I could do this?

The javascript modules seem like a different use case to me.. for instance, you can load jquery and then in the function param use it.. but I just need to load the script like it was in a head tag. Can I do this with a Javascript module?


Many Thanks,

Matt

In reply to Matt Sawatzky

Re: Issue loading external script with requirejs

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Remove the 'https' at the start of the URL so that it just starts with '//'.

In reply to Matt Sawatzky

Re: Issue loading external script with requirejs

by Justin Hunt -
Picture of Particularly helpful Moodlers Picture of Plugin developers
Matt


How are you adding that Javascript? Are you editing a PHP file in core moodle or a plugin, or are you just trying to get it through the HTML editor? If editing-plugin/hacking-core you are on the right track though getting non AMD modules to play nicely with requirejs can be tricky. 

Anyway, the Generico filter was created for purposes like this, where you just need a little front end insert and do not want to create a whole new plugin. You create templates and add your external javacript links in there. Perhaps check it out and see if it helps. 

https://moodle.org/plugins/filter_generico

There are quite a few templates available as examples. Some of them are here:

https://moodle.org/plugins/browse.php?list=category&id=69