A lot of pages in coppermine uses javascript for client side enhancements and validations. This guide will help developers to understand how javascript is organized in coppermine.
This part of the documentation is not meant for end users of Coppermine, but only for developers. There is no support for this section, it comes as-is.
There is still lot of javascript inline with html (from cpg1.4). This javascript needs to be separated into their own files
Javascript files can be included from within php code by calling js_include() function.
// This line of code will include displayimage.js file js_include('js/displayimage.js');
If you need to add a JavaScript file for a plugin, the 'page_start' plugin action might be used.
$thisplugin->add_action('page_start','custom_function_to_include_js'); function custom_function_to_include_js() { global $JS; // Don't forget to make that variable global when using from inside functions $JS['includes'][] = 'plugins/your_plugin/your_javascript_file.js'; }
Technically, inline JavaScript is possible as well, but not recommended.
If you need to pass dynamic data from PHP code (e.g. a language string that resides in a PHP variable) to the JavaScript code that resides in an external file included with the method explained above, you can use the function set_js_var() that get's defined in include/functions.inc.php.
<?php $foo = 'bar'; set_js_var('php_foo', $foo); ?>
Keep in i18n in mind, especially for the strings passed from language files: be particularly mindfull on properly escaping single quotes (apostrophes) that may reside in other languages than the one you're testing with.
In "traditional" JavaScript, there are several methods to make JavaScript code execute when the page is being loaded, using the pageLoad event. This used to cause issues and incompatibilities. With cpg1.5.x using the jQuery library, it has become very straightforward to come up with JavaScript autostarts: every piece of JavaScript code that you want to see executed on page load should use the "document ready" construct: wrap all autostart code into
$(document).ready(function(){and
});
The advantage of this method is that jquery will take care of triggering the autostart functions - there can be several of those "document ready" constructs, even in different .js files.