How to include javascript in head tag

How to include javascript in head tag

by Stanimir Genov -
Number of replies: 7

I found in the documentation that javascript files can be included like this:

// Print title and header
$PAGE->set_pagelayout('standard');
$PAGE->set_title("$course->shortname: График");
$PAGE->set_heading($COURSE->fullname);
$PAGE->requires->css($CFG->dirroot.'/grafik/javascript/tcal.css');
$PAGE->requires->js($CFG->dirroot.'/grafik/javascript/language.js',true);
$PAGE->requires->js($CFG->dirroot.'/grafik/javascript/tcal.js',true);
echo $OUTPUT->header();

However, by doing this in one of my modules, I get this error:

Coding error detected, it must be fixed by a programmer: Attempt to require a JavaScript file that does not exist.

More information about this error

Debug info: /var/www/moodle/grafik/javascript/language.js
Stack trace:
  • line 368 of /lib/outputrequirementslib.php: coding_exception thrown
  • line 325 of /lib/outputrequirementslib.php: call to page_requirements_manager->js_fix_url()
  • line 37 of /grafik/view.php: call to page_requirements_manager->js()

 

But the file is right there as you can see:

root@dreamwave-Aspire-5738:/var/www/moodle/grafik/javascript# ls
img index.html language.js tcal.css tcal.js

Why is this happening ? How do I include my js files?

Average of ratings: Useful (1)
In reply to Stanimir Genov

Re: How to include javascript in head tag

by Benjamin Ellis -
Picture of Particularly helpful Moodlers

Point them at the web address rather then the file path???  Top of my head.

In reply to Benjamin Ellis

Re: How to include javascript in head tag

by Patrick Pollet -

Hello,

  The error occurs at line 325 of script lib/outputrequirements.php script in method named js  ; if you read comments at the top you will see that path to JS must be relative to $CFG->dirroot 

* @param string|moodle_url $url The path to the .js file, relative to $CFG->dirroot / $CFG->wwwroot.
* For example '/mod/mymod/customscripts.js'; use moodle_url for external scripts

So you should write 

$PAGE->requires->js(/grafik/javascript/language.js',true);

Strangely you had no fatal error in the previous line ?

$PAGE->requires->css($CFG->dirroot.'/grafik/javascript/tcal.css');

If you read comments at the top of function css in the same script you will see

* @param string $stylesheet The path to the .css file, relative to $CFG->wwwroot.
* For example:
* $PAGE->requires->css('mod/data/css.php?d='.$data->id);

that is it also must be relative ... 

Cheers.

 

 

In reply to Stanimir Genov

Re: How to include javascript in head tag

by Mahendra Soni -

Don't add  $CFG->dirroot.

Simply try

$PAGE->requires->js('/grafik/javascript/language.js',true);
$PAGE->requires->js('/grafik/javascript/tcal.js',true);

still getting problem then

try add JS to theme folder:

like this

step-1: theme/standard

step-2: create a folder "javascript" if not already exist.

step-3: copy all js here in javascript folder.

step-4: opne config.php file located at theme/standard/config.php

step-5: Add this line at the end of the code

$THEME->javascripts_footer = array('language','tcl');

note: You don't need to add .js extension;

step-6: purge all caches(clear the caches).

It will work now.

all the best

smile smile

In reply to Mahendra Soni

Re: How to include javascript in head tag

by Mark Ward -

Yes, remember that javascript is a client side script so /var/www/moodle/grafik/javascript... is not correct. You need to provide the location of the file as if you were accessing it through a web browser, like http://www.moodle.org/grafik/javascript.

Mahendra's code look right, you dont need to include the $CFG->wwwroot or $CFG->dirroot as moodle will handle that for you. So long as you have a folder in the base of your Moodle called grafik which contains javascript/language.js then $PAGE->requires->js('/grafik/javascript/language.js'); should work.

Other thing to check is whether your javascript itself is working properly, you can check that in Firebug or some other web development console.

In reply to Mahendra Soni

Re: How to include javascript in head tag

by Stefan Pantic -

When I add the second param (true), the script is  not included on my page (whole page) ... what's the problem here??

In reply to Stefan Pantic

Re: How to include javascript in head tag

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Stefan, you seem to be resurrecting a very old thread to ask your question.

You also haven't provided very much information to go on:

  • What line of code did you write?
  • What happens when you leave out the second param?
  • If it works when you leave out the second param, then what is the problem you are trying to address?
  • Are you trying to include the javascript after the page head has already been sent?


In reply to Davo Smith

Re: How to include javascript in head tag

by Stefan Pantic -

Oh, I didn't see tha date... :D
I am developing a new question type. And when my qtype renders a question, it needs a external js file (a library). When using:

$PAGE->requires->js('url',true);

the problem was because the head was already sent.

So I made an AMD module with requirejs, and that solved my proble.

Also, I would like to help others, if they similar problem; there is a method head_code() from qtype_renderer class, which will return any HTML that needs to be included in the page's <head> when this question is used.