6

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>
user2864740
  • 60,010
  • 15
  • 145
  • 220
Solace
  • 8,612
  • 22
  • 95
  • 183
  • Just put it into the template? – Ulrich Thomas Gabor Aug 09 '14 at 13:45
  • 1
    Where do you want add your external js in a module or theme? – Suman Bogati Aug 10 '14 at 09:39
  • @SumanBogati I want to display it on the `attempt_form` in the page where the student attempts a question in a quiz. It is the `attempt.php` file in the `quiz` module. Actually to render the output, `attempt_page()` function of the quiz `renderer` is called from `attempt.php`. This method calls the `attempt_form` method. This is exactly where I need to load the JS. It is actually a TabView (from YUI JS library) which I want to display. I want to use [this code](http://css-tricks.com/simple-tabbed-box/), but I do not know where to put different bits of it. – Solace Aug 10 '14 at 18:03

2 Answers2

6

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'));
sumit
  • 332
  • 2
  • 7
4

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>

  • 1
    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! – Manuel Fernando Sep 12 '15 at 04:45