Error including javascript

Error including javascript

Craig Hagerman發表於
Number of replies: 2

I am writing a custom module that uses some javascript. As I understand it for >2.0 I can put this in to view.php

$PAGE->requires->js('mod/mymodule/javascript/sr.js',true);

But this gives an error "Coding error detected, it must be fixed by a programmer: Invalid JS url, it has to be shortened url starting with / or moodle_url instance."

I changed the code by adding an initial slash:

$PAGE->requires->js('/mod/mymodule/javascript/sr.js',true);

and there is then no error. But all of the examples and details I have seen on the web use the first form (without the leading slash). Just wondering if this has changed in the most recent versions of moodle (I'm using 2.4.1)?

 

評比平均分數: -
In reply to Craig Hagerman

Re: Error including javascript

Tim Hunt發表於
Core developers的相片 Documentation writers的相片 Particularly helpful Moodlers的相片 Peer reviewers的相片 Plugin developers的相片

I think the docs are probably wrong, and the code with the slash is right.

An even better way to do your code is http://docs.moodle.org/dev/How_to_create_a_YUI_3_module.

In reply to Tim Hunt

Re: Error including javascript

Craig Hagerman發表於

Thanks Tim. I was just looking for confirmation. This is my first experience developing a moodle module and I have found I've spent a lot of time digging around in the code to try to figure out how things work and why. I'll probably re-factor and do my existing javascript the YUI3 way as an exercise after I get the plugin finished.