Take the 2-minute tour ×
Joomla Stack Exchange is a question and answer site for Joomla! administrators, users, developers and designers. It's 100% free, no registration required.

I use an adapted version of google code prettify on my page which lets me highlight Mathematica code. For the correct injection of the necessary js code, the JPrettify plugin is quite handy. Since this plugin seems to be no longer officially available and since I need to tweak it anyway, I set up my own version on GitHub.

Yesterday, I was pointed to the fact that it doesn't work in Joomla 3 and after some debugging we found out that the function PrettyPrint is not called when it is inserted as follows in the html head:

window.addEvent('domready', function() { prettyPrint();});

This is they way the JPrettify (line 53) does it. After some googling, it turns out that the problem is the mootools library. We solved the problem by statically inserting

<body onload="PrettyPrint()">

into the main template php file, which is of course not the right way to go.

Question: To make the plugin work through different versions of Joomla, what is the best method to call PrettyPrint()?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Rather than doing body onload, the following should work:

$(document).ready(function() {
    PrettyPrint();
});

Or of you would rather use Javascript to determine when the function is called, you could use :

window.onload = function() {
    PrettyPrint();
};
share|improve this answer
    
Thanks. I guess I use the second variant. –  halirutan Jun 11 at 14:46

There is a ongoing work within Joomla 3 to remove Mootools based scripts and replace them with jQuery ones. The main issue why your code fails is that you don't load Mootools yourself. In prior versions of Joomla, it may have been loaded by the main layout or a module, and now it is no longer loaded because the scripts don't need it anymore.

So if you need it for your module, load it using JHtmlBehavior::famework() (http://api.joomla.org/cms-3/classes/JHtmlBehavior.html#method_framework).

The same applies if you rewrite your script to use jQuery. Don't count on it being loaded, tell Joomla that you need it. Joomla will take care that it's only loaded once. To load jQuery, you'd use JHtmlJquery::framework() (http://api.joomla.org/cms-3/classes/JHtmlJquery.html#method_framework).

share|improve this answer
    
But the solution given by Lodder with window.onload doesn't use JQuery, right? window.onload is standard javascript, yes? –  halirutan Jun 11 at 17:00
    
@halirutan - yes, window.onload is pure Javascript –  Lodder Jun 11 at 17:07
    
@halirutan: Although if you are using "standard JavaScript", then you should probably be using addEventListener, rather than assigning directly to the onload property. –  w3d Jun 11 at 18:40
    
@w3d - Good point, but I believe addEventListener isn't supported for IE8. For IE8 you will have to use attachEvent. Not sure about window.onload support as I haven't tested this in older IE browsers. –  Lodder Jun 11 at 19:03
    
@Lodder: Yes, for IE8 support you'll need to use attachEvent (a reason for using a JS framework like jQuery / MooTools that iron out these differences). window.onload will work in all browsers, old and new, but it has its own problems... can only assign one event (a problem with multiple scripts / larger environments, as events could get overwritten), or the value of this is changed when wrapping in another function (as above). –  w3d Jun 11 at 23:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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