forum.coppermine-gallery.net

Support => cpg1.4.x Support => Older/other versions => cpg1.4 plugins => Topic started by: Gwyneth Llewelyn on August 15, 2009, 12:33:54 pm

Title: Adding JavaScript to <head>...</head> — using $JS or the gallery_header?
Post by: Gwyneth Llewelyn on August 15, 2009, 12:33:54 pm
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 (http://documentation.coppermine-gallery.net/en/dev_javascript.htm#dev_javascript_include_files_plugin), and call the page_start hook to get it done:

Code: [Select]
$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.
Title: Re: Adding JavaScript to <head>...</head> — using $JS or the gallery_header?
Post by: Joe Carver on August 15, 2009, 01:04:30 pm
If you want to include an external .js in your <head> then simply use:

Code: [Select]
$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;
}
Title: Re: Adding JavaScript to <head>...</head> — using $JS or the gallery_header?
Post by: Gwyneth Llewelyn on August 15, 2009, 03:59:39 pm
Awesome, thanks!! It works like a charm!
Title: Re: Adding JavaScript to <head>...</head> — using $JS or the gallery_header?
Post by: Joachim Mόller on August 17, 2009, 08:37:07 am
The manual — which seems to reflect the yet-unreleased 1.5 version only — suggests that the $JS variable should be used inside plugins instead (http://documentation.coppermine-gallery.net/en/dev_javascript.htm#dev_javascript_include_files_plugin), 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.