Adding JavaScript to <head>...</head> — using $JS or the gallery_header? Adding JavaScript to <head>...</head> — using $JS or the gallery_header?
 

News:

cpg1.5.48 Security release - upgrade mandatory!
The Coppermine development team is releasing a security update for Coppermine in order to counter a recently discovered vulnerability. It is important that all users who run version cpg1.5.46 or older update to this latest version as soon as possible.
[more]

Main Menu

Adding JavaScript to <head>...</head> — using $JS or the gallery_header?

Started by Gwyneth Llewelyn, August 15, 2009, 12:33:54 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Gwyneth Llewelyn

Hi,

While trying to tweak things on the Coppermine codebase as little as possible, as well as keeping themes as they are, I'm trying to move all my mods into a simple collection of plugins. Some changes are really as simple as a couple of PHP lines — nevertheless, every time I need to upgrade Coppermine, it's a pain to figure out where things are. Also, tweaking template.html or theme.php is not always a good idea, since (the default) themes also get updated every time Coppermine gets an update...

One simple issue I've got is that I need to add some external Javascript (possibly not on all pages). The simplest way to do so is, of course, to hack template.html, but, as said, this can be a pain for some themes — even if I do the changes from within a plugin, this can fail for some themes.

The manual — which seems to reflect the yet-unreleased 1.5 version only — suggests that the $JS variable should be used inside plugins instead, and call the page_start hook to get it done:


$thisplugin->add_action('page_start','add_javascript');

function add_javascript()
{
global $JS; // Don't forget to make that variable global when using from inside functions
$JS['includes'][] = 'one-external-javascript-file.js';
$JS['includes'][] = 'another-external-javascript-file.js';
[...]
}


Sadly, this gets completely ignored by Coppermine 1.4.X. Probably it's only functional in 1.5, I don't know.

Some plugins use a different hook, either gallery_header or gallery_footer instead. This, however, will not place your Javascript inside the <head>...</head> section, but below <body>, somewhere where the template.html defines where the actual gallery code is supposed to start.

So, how do you include external JavaScript files in Coppermine 1.4.X inside the header? The answer doesn't seem to be "use js_include" as many have suggested in this forum; at least for 1.4.25, this will fail with an error.

Joe Carver

If you want to include an external .js in your <head> then simply use:

$thisplugin->add_filter('page_meta','name_of_filter_and_function');


function name_of_filter_and_function($html) {
    $html = '<script type="text/javascript" src="your_external.js"></script>'
            .$html;
    return $html;
}

Gwyneth Llewelyn


Joachim Müller

Quote from: Gwyneth Llewelyn on August 15, 2009, 12:33:54 PM
The manual — which seems to reflect the yet-unreleased 1.5 version only — suggests that the $JS variable should be used inside plugins instead, and call the page_start hook to get it done:
cpg1.5.x goes unsupported. The documentation for that version goes unsupported as well, so you shouldn't have refered to it. The word "unsupported" means exactly what it says.
Indeed the technique you posted only applies for cpg1.5.x. The trick posted by i-imagine works for cpg1.4.x as well as for cpg1.5.x, with the drawback that this happens on all coppermine-driven pages, which can be a drawback depending what your plugin is suppossed to do.