Forums | Mahara Community

Developers /
Javascript in the footer ?


21 July 2017, 22:30

Hello !

I have two questions innocent

 

1. INCLUDES

How can include a js file in a plugin ?

Because I add it in the core in lib/web.php like this ;

javascript_array[] = $jsroot . 'echarts.min.js';

But I do not like this method, I would like a way to insert the file into my plugin.

 

2. JS IN THE FOOTER

I use $INLINEJAVASCRIPT like this :

$smarty->assign('INLINEJAVASCRIPT', $my_js);

But is there a way to put the script in the footer of a page ?

 

Thank you ! cool

Robert Lyon's profile picture
Posts: 774

24 July 2017, 8:07

Hi

For Question 1:

If the plugin is of type blocktype then you can add to your block lib.php file

    public static function get_instance_javascript(BlockInstance $bi) {
        return array(
            array(
                'file'   => get_config('wwwroot') . 'echarts.min.js',
            )
        );
    }

See artefact/blog/blocktype/taggedposts/lib.php for an example (it also shows how to call a javascript function on load of block)

For Question 2:

If you are needing the javascript to load after the page loads the best way to do that is to wrap your $my_js javascript inside of:

jQuery( function() {
   [my current code]
});

This make the code inside it execute after the page is fully loaded

Cheers

Robert

 

2 results