How to Allow Modules to Save JavaScript Code in Joomla 2.5

Yesterday we were working on the migration of a very old website. That website had a simple module that was programmed to display, on the website, whatever it had in the field JavaScript Code. As the name suggests, that field contained some JavaScript code. When we migrated the module to Joomla 2.5, we ran into a small issue: <script> tags were stripped from the code whenever we hit on Save. We weren’t using any editor for the field and we were logged in as super administrators and the Filter Type for super administrators was set to No Filtering (you can find the Filter Type field by logging in to the backend, and then clicking on Site -> Global Configuration on the upper top menu, and then clicking on Text Filters on the middle top menu) which means that there shouldn’t be any logical reason why Joomla would strip the JavaScript code. But it was…

So we investigated a bit, and we discovered that Joomla 2.5, was set to clean (e.g. filter) all the input submitted through modules, unless it was told not to… So, the question is now, how do you tell Joomla not to do so?

Well, it’s very easy. All you need to do is to modify the filter type of the field (that you wish to add the JavaScript code to) in the manifest (the manifest file is the XML file that defines the extension. [which fields are needed, what are their types, which files should be included with the extension, etc…] Every extension must have an XML file, which must be located in the root of the installation package, and must have the same name as the name of the extension.) The filter type must be set to raw.

For example, if the following line defines the field javascript_code in your manifest file:

<field name="javascript_code" type="textarea" rows="20" cols="80" default="" label="JavaScript Code" description="Add your JavaScript code here" />

Then it should be changed to:

<field name="javascript_code" type="textarea" rows="20" cols="80" default="" filter="raw" label="JavaScript Code" description="Add your JavaScript code here" />

And that’s it! Now your module can save any code!

While we think that the above can be done by a non-programmer, we have to warn you that making a mistake anywhere can break your module, so it’s better to make a backup that you can revert to in case something goes wrong. If you would rather do this work yourself (which is admittedly a bit technical), then, as usual, you can contact us! We’re always there, we’re always happy to serve, we’re Joomla experts, and we our fees are very affordable!

No comments yet.

Leave a comment