How to include external JavaScript on my Moodle page?

11,293

Solution 1

Use following code to include javascript file in your code:

$PAGE->requires->js() 

E.g

$PAGE->requires->js( new moodle_url($CFG->wwwroot . '/local/my_localplugin/myjavascript.js'));

Solution 2

You can use the following code to include a javascript file in your code inside the :

$PAGE->requires->js('/mod/namemodule/socket.io.js',true);

In this way, the file is loaded socket.io.js within the <head> </head>

Share:
11,293
Solace
Author by

Solace

Updated on June 06, 2022

Comments

  • Solace
    Solace about 2 years

    In Moodle, while we use $PAGE->requires->js_init_call() to include a JS function defined in our plugin's module.js file, how can I include external Javascript resources, which we normally include in the <head> tags like:

    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    
    <title>mypage</title>
    
    <script type="text/javascript" src="http://yui.yahooapis.com/2.5.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
    
    <script type="text/javascript" src="http://yui.yahooapis.com/2.5.0/build/element/element-beta-min.js"></script>
    
    <script type="text/javascript" src="http://yui.yahooapis.com/2.5.0/build/connection/connection-min.js"></script>
    
    <script type="text/javascript" src="http://yui.yahooapis.com/2.5.0/build/tabview/tabview-min.js"></script>
    </head>
    
  • sneilan
    sneilan over 9 years
    This is not the answer to this question.
  • Manuel Fernando
    Manuel Fernando almost 9 years
    In Moodle 2.0 used $PAGE->requires->js(). Make $PAGE available to your code by doing: require_once($CFG->libdir . '/pagelib.php'); global $PAGE; Add our code: $PAGE->requires->js( new moodle_url($CFG->wwwroot . '/mod/mymod/script.js') ); It is required to put moodle_url() around your path!