Page 1 of 1

How do I add Javascripts?

Posted: Thu Feb 23, 2012 5:46 pm
by erpee
I'm building my first module.

This module use Jquery and Javascript.

When the module is called, Jquery an Javascript must be loaded in the HEAD.

Code: Select all

<link rel="stylesheet" href="path/Style.css" />
<__script__ src="path/jquery-1.4.3.min.js"></__script>
How can I realise that when the module is loaded, the code above is placed in the HEAD?

Re: How do I add Javascripts?

Posted: Thu Feb 23, 2012 7:38 pm
by Wishbone
Take a look at the Gallery module.. It registers a trigger that gets called when the final HTML output is generated. It then inserts all the JavaScript and CSS stuff right before the </head>

Re: How do I add Javascripts?

Posted: Fri Feb 24, 2012 4:11 am
by calguy1000
Take a look at the Gallery module.. It registers a trigger that gets called when the final HTML output is generated. It then inserts all the JavaScript and CSS stuff right before the </head>
And this is exactly the WRONG way to do this. Because it does not leave control up to the developer to manage this stuff. What were to happen (for example) if you wanted two galleries with two different templates (and therefore two different sets of javascript requirements).. or two different modules that needed javascript on the same page.

The proper way to handle this is to give the developer control over the issue. Typically this is done by another action in the module... like {mymodule action=incjs} or something that can be placed in the page template header.

IIRC the Album module does this in a smart way... when the album module is called in the body, the template that is activated is 'saved', and when the {Album action='incjs'} (or whatever it is) is called, it detects that 'saved' template and knows what javascripts/stylesheets to include etc. But it also does not get in the way of the developer removing that capability, or doing something else entirely.

Re: How do I add Javascripts?

Posted: Fri Feb 24, 2012 5:39 am
by Wishbone
I like the automatic inclusion, for ease of use, but think it would be good to have the option to manually place it as well, in case you need to put it in a particular spot.

Re: How do I add Javascripts?

Posted: Fri Feb 24, 2012 11:05 am
by Jos
calguy1000 wrote:And this is exactly the WRONG way to do this.
:-X
calguy1000 wrote:it does not leave control up to the developer to manage this stuff.
Not true: Gallery provides developers a way to control what the module puts in the head section.