Writing javascript in the head section of a mod view page

Writing javascript in the head section of a mod view page

by Matt Bury -
Number of replies: 11
Picture of Plugin developers

Hi,

Forgive me if this has been asked before. I've searched but haven't found the particular answer I'm looking for:

I want to assemble a snippet of JS code that needs to go in the head section of a mod view.php page. For example:

<script src="js/swfobject.js" type="text/javascript" charset="utf-8"></script>

<script src="js/swfaddress.js" type="text/javascript" charset="utf-8"></script>

<script type="text/javascript">

var flashvars = {};

var params = {};

var attributes = {};

swfobject.embedSWF("file.swf", "myAlternativeContent", "900", "580", "9.0.115", "js/expressInstall.swf", flashvars, params, attributes);

</script>

I've found stuff about including links to JS files but nothing on actually printing code. The JS code MUST go in the head section of the page, anything else just won't work. The JS is also assembled dynamically from Moodle DB entries so that parameters can be set. In other CMS' this is a fairly simple and common procedure but I can't work out how to do it in Moodle.

BTW, in Moodle 1.9 it was a case of doing:

print_header_simple(format_string($swf->name), '', $navigation, '', $swf_javascript_code, true, update_module_button($cm->id, $course->id, $strswf), navmenu($course, $cm));

Thanks in advance!

Average of ratings: -
In reply to Matt Bury

Re: Writing javascript in the head section of a mod view page

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I am surprised you could not work it out yourself. Here is now I worked it out:

  1. In 1.9, the mechanism for including JavaScript was not that nasty hack using the $meta argument of print_header, but was actually a function called something like require_js (if I remember correctly)
  2. In 2.x, the equivalent of require_js is a call like $PAGE->requires->..., and the various things that can come after that are the methods of the page_requirements_manager class defined in lib/outputrenderers.php
  3. So, go and start looking at the PHPdoc comments there, and find $PAGE->requires->js($url, true); will do what you want.
In reply to Tim Hunt

Re: Writing javascript in the head section of a mod view page

by Matt Bury -
Picture of Plugin developers

Hi Tim,

Thanks for the help. I'd already found the $PAGE->requires->js($url, true); and this page: http://phpdocs.moodle.org/HEAD/core/lib/page_requirements_manager.html

What's the function to include a string of JS in the head? I can't seem find it.

Thanks.

In reply to Matt Bury

Re: Writing javascript in the head section of a mod view page

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Well, read the code in lib/outputrequirementslib.php. Particularly the code in get_head_code(), to see where it gets information from.

My reading of that is that your best bet is to use or abuse data_for_js.

In reply to Tim Hunt

Re: Writing javascript in the head section of a mod view page

by Matt Bury -
Picture of Plugin developers

Hi Tim,

The page I linked to states:

data_for_js (line 889)

!!!!!!DEPRECATED!!!!!! please use js_init_call() for everything now.

Has anyone worked out a satisfactory solution?

In reply to Matt Bury

Re: Writing javascript in the head section of a mod view page

by Matt Bury -
Picture of Plugin developers

*bump*

In reply to Matt Bury

Re: Writing javascript in the head section of a mod view page

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

If you are using a deprectated coding pattern that requires JavaScript in the <head> tag, then why do you object to using a deprecated Moodle API. It still works, so use it for now, and plan to rewrite all your JavaScript later so that it does not suck.

In reply to Tim Hunt

Re: Writing javascript in the head section of a mod view page

by Matt Bury -
Picture of Plugin developers

*bump*

In reply to Matt Bury

Re: Writing javascript in the head section of a mod view page

by Edmund Edgar -

Hi Matt.

This won’t answer your specific question, but I ran into some similar issues porting some code to Moodle 2, and after restructuring the code to work in a way that didn’t shock Moodle, I ended up liking the Moodle-2-ified thing better than the original.

Could you post a link to a simple HTML page showing of what you’re trying to do in the working, pre-Moodle-2-idiosyncracity version? (I guess most of the code is already in your post, but I mean in an HTML page with an actual swf file embedded so we can see it in action.) We may be able to help you rework it in a way that the Moodle APIs find less scandalous.

In reply to Edmund Edgar

Re: Writing javascript in the head section of a mod view page

by Matt Bury -
Picture of Plugin developers

Hi Edmund,

I'm using SWFObject to embed SWFs. It's pretty much the standard way to do it these days. Docs are here: http://code.google.com/p/swfobject/wiki/documentation

I can understand Moodle developers wanting to discourage developers from including a lot of code in the head section but prohibiting it seems a bit extreme. SWFObject is small and lightweight and doesn't have a big impact on page run times, compared to some of the JS that Moodle pages automatically load.

In reply to Matt Bury

Re: Writing javascript in the head section of a mod view page

by Edmund Edgar -

I'm not going deny that I sometimes get frustrated with Moodle for its tendancy to prioritize "Stop developers from cutting their own toes off" over "Principle of least astonishment", but this seems to work with the page structured in a way that you should be able to ask the Moodle 2 APIs to output it without them reaching for the smelling salts:

http://dev7.socialminds.jp/swfobjtest/swfobject-read-only/swfobject/index_dynamic_foot.html

Or am I missing your point?

In reply to Edmund Edgar

Re: Writing javascript in the head section of a mod view page

by Matt Bury -
Picture of Plugin developers

Hi Edmund,

Thanks for the link. I'm not sure I understand your message though.