1

I use a lot of javascript on my site. It works and has passed crossbrowser testing, but the thing is the javascript files are being included within Joomla articles. I’ve read that this is the wrong way to go about it, but I don’t know why and am not sure of a better way to do it (I believe one reason had to do with the javascript not being included in the head of the webpage).

IMPORTANT I’ve considered including the javascript in the template, but each Joomla article can be vastly different. While I try to be consistent with articles in similar categories this isn’t always possible. If I were to include the javascript into the template I would need to create a different template for each category type and then I’d still need to include the odd script into a few articles.

Perhaps I should include the javascript via custom html modules? I’m not really sure what benefit this would provide over the current method.

QUESTIONS

  • Should javascript not be included via joomla articles? If yes, why?
  • How would you include javascript in this situation? Remember that each article can use vastly different js.

EDIT1

I'm particularly interested in how people would recommend I add the javascript to Joomla in my situation.

4
  • What is your use case that you need to put JavaScript in articles? Jul 5, 2014 at 17:03
  • Not sure exactly what you mean by use case, but I'm the only person who can edit articles. This is likely to continue indefinitely. I don't use WYSIWYG editors, I just write it in html. The included JS is for functionality.
    – TryHarder
    Jul 6, 2014 at 12:00
  • OK, but how about writing the code in a .js file, putting it under the media folder and including it from there? Maybe building everything around JavaScript objects and reducing the the code duplication (where possible). If you ever need to update something, you just need to edit one fine, not tons of articles. My 2c. Jul 7, 2014 at 9:26
  • Sorry, I probably haven't been very clear. The js is stored in external javascript files and included into the website via the Joomla articles. Regardless I agree with you- it'd be a nightmare to edit thousands of articles.
    – TryHarder
    Jul 7, 2014 at 10:34

3 Answers 3

4

If you're including the JavaScript as a user and customized to your article, then it's fine. Especially if it is different for each article.

However if you happen to repeat the same JavaScript code in multiple articles, then there may be better solutions like an alternative article layout.

You can read more about alternative layouts here: http://docs.joomla.org/Layout_Overrides_in_Joomla. Basically you copy the orginal layout file to your template like you would do to override it. But then you rename the file to a new name. This new layout can then be selected as an alternative layout for an article. You then can add the JavaScript to that layout and reuse it for each article you want.

Another way would be to use either the custom module or any of the available plugins/module which allow to add assets to the site. However personally I think I would use alternative layouts.

If we are speaking about extension developers who ask the users to include JavaScript code in the articles, then those developers should be fired :-)

4
  • I'm option 1 and 2. Could you elaborate more on the alternative article layout?
    – TryHarder
    Jun 10, 2014 at 14:06
  • or even on the alternative solutions? I've been thinking of adding scripts via the custom html module, but I'm not sure that there is much benefit in doing that. I guess if I get really desperate I could write a plugin.
    – TryHarder
    Jun 10, 2014 at 14:15
  • Added some additional infos
    – Bakual
    Jun 11, 2014 at 9:59
  • Thanks for the update and links. I'd never heard of alternative layouts till your post.
    – TryHarder
    Jun 12, 2014 at 11:30
6

Articles are for content, not source code.

Rule of thumb, different content in different places.

If I have to quickly include Javascript, I would recommend NoNumber Sourcerer: http://www.nonumber.nl/extensions/sourcerer

DRY principle, don't repeat yourself. Create modules with the Javascript code, and include it with {loadposition module-position}

3
  • So the only reason to not include via articles is because of the DRY principle? or are you saying that the reason to not include javascript is because people say you shouldn't? Will javascript included via modules be included in the head of the webpage? or is this dependent on the module position within your template?
    – TryHarder
    Jun 10, 2014 at 1:49
  • I guess what I am saying is, I understand the principle behind keeping things separate, but are there any other reasons for not including js via the joomla articles- some tangible reasons not bound by theory.
    – TryHarder
    Jun 10, 2014 at 1:56
  • 1
    Joomla API only allows to insert Javascript in the head. Bootstrap, Google Analytics, etc insert Javascript initialization just before </body>. At the end, it depends on your view how to manage it. I think the best rule is avoid copy-paste. In next steps, you can create a better structure to integrate your scripts.
    – Anibal
    Jun 10, 2014 at 2:59
3

Should javascript not be included via joomla articles? If yes, why?

I would advise gently against using JS straight up in articles...

  • Not user-friendly - many editors will wipe out JS written inline (maybe not when you're using it, but when your lower-permission users do who aren't allowed to save articles with code in them...)
  • Can't combine / minify (without additional plugins / components) - it also adds extra weight to a site (can't combine scripts together) and minifying / unminifying it to make changes is inefficient and time-consuming
  • Blocking behaviour - JS in the body will block until it's finished. If it's complex, you may have users waiting for it to complete for the rest of the page to load
  • And, of course, it's ugly - whilst it's not the worst thing in the world, it's also the hackiest of hacky solutions given the many, many different ways Joomla has to include JS on a page.

How would you include javascript in this situation? Remember that each article can use vastly different js.

I'm curious - what requires every article to have 'vastly different JS'? Surely there must be generalizations you can make?

7
  • Are you sure that you can't combine/minify scripts included via articles? If true, this is a very good reason for me to change my ways. Regarding the articles, I was trying to avoid people from replying with stock standard answers of "put it in your template." The articles aren't as bad as I made them out to be, but it's still a problem. I'll need to create a lot of templates to cover all possible categories. I don't think this unusual, it's not a blog. I hope you edit your 4th point, I was hoping to avoid opinionated answers. Apart from that I like this answer. Thanks.
    – TryHarder
    Jun 10, 2014 at 9:50
  • Just to be clear, I'm not against putting it in the template, but I'm not a fan of creating multiple templates just to include the js. There may be some other way of achieving both things.
    – TryHarder
    Jun 10, 2014 at 9:53
  • Fair enough - most of the combine methods I know collate the scripts server-side then pump out a single JS file. Minify you can obviously do manually in each article or find a server-side preprocessor to look at the output of your page and do it there, but these seem like taking a sledgehammer to crack a nut... Jun 10, 2014 at 11:22
  • Point 3 is not necessarily true, I often add scripts asynchronously allowing my articles to load without the blocking that you mentioned.
    – TryHarder
    Jun 10, 2014 at 14:19
  • That's true, if it's in a document ready call. Jun 10, 2014 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.