How do I add Javascripts?

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
User avatar
erpee
Power Poster
Power Poster
Posts: 771
Joined: Sat Jul 07, 2007 9:22 am
Location: Almelo, The Netherlands

How do I add Javascripts?

Post 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?
Waarom zou het direct goed moeten gaan? Dan is alle lol weg ...
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: How do I add Javascripts?

Post 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>
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: How do I add Javascripts?

Post 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.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: How do I add Javascripts?

Post 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.
Jos
Support Guru
Support Guru
Posts: 4019
Joined: Wed Sep 05, 2007 8:03 pm
Location: The Netherlands

Re: How do I add Javascripts?

Post 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.
Post Reply

Return to “Developers Discussion”