Third-party Javascript

Hi everyone.

I hope you had a good weekend.

I want to know how to use a third-party javascript in elgg. I am building module, and I am using more than one js file for my module. How to integrate such external js file in elgg ??

Could someone told me me where the jquery libraries are located in elgg ?

I read that it is recommended to use AMD (Asynchronous Module Definition) standard for writing JavaScript. I want to kown if I can run a non AMD (Asynchronous Module Definition)  JavaScript code in elgg.

Best regard

  • If you are using more than one JS file, you should be using AMD, as it will ensure that no one script runs before its dependencies are loaded.

    If the external file is an exportable AMD module, you should register it:

    // Register the script for synchronous loading in your start.php
    elgg_register_js('external_script_name', $external_url);
    
    // Load the script where you need it
    elgg_load_js('external_script_name');
    

    Most scripts these days come as AMD module, e.g. moment.js, so you can register them and require them in your script:

    // Define the module
    elgg_define_js('moment', [
       'src' => $cdn_url,
       'exports' => 'moment',
    ]);
    
    // In your AMD module
    define(function(require) {
       var moment = require('moment');
       var $ = require('jquery');
    });
    

    You shouldn't need to know the location of jQuery. If you do, then you are doing something wrong.

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking