I'm assuming you're using Joomla1.0?
Joomla buffers content using the PHP output buffer. ie: ob_start().
You can get the contents using: ob_get_contents();
Thus you can have a regular expression that checks for JQuery. Something like:
$jquery_loaded = preg_match("/<script.*?src=[\"']jquery[^\"']\"/i", ob_get_contents());
should be good enough. (I haven't tested that).
Using ob_get_contents() may not work in situations since the PHP output buffer can be nested. You can start as many buffers within buffers.
For Joomla1.5, you can get the buffer via the API which ensures you're always getting Joomla output.
$Document =& JFactory::getDocument();
$buffer = $Document->getBuffer();
Whether Joomla1.0 or 1.5 you have to note that Joomla can add to the buffer at any point before it renders the output (calls ob_flush() or equivalent). Thus your check for JQuery has to take into account that JQuery can be loaded after the check.
Note that Joomla buffer is created not only for HTML, but can be CSS, JavaScript, XML, JSON etc. So you might want to check for HTML before doing your JQuery tests. You can also test for the admin panel.
$mainframe =& JFactory::getApplication();
$doctype = $document->getType();
// deactivate for backend
if ($mainframe->isAdmin() || $doctype != 'html') {
return false;
}
For reference here is an example system plugin that partly does the things you want. It is a compatibility plugin for MooTools1.2.
<?php
/**
* MooTools1.2 w/ 1.1 compat for AjaxChat
* @copyright www.fijiwebdesign.com
* @author [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*/
// included only
defined( '_JEXEC' ) or die( 'Direct Access to this location is not allowed!' );
jimport( 'joomla.plugin.plugin' );
/**
* Joomla PHP Speedy Integration
*
* @author [email protected]
*/
class plgSystemAjaxchat extends JPlugin
{
/**
* Constructor
*
* For php4 compatability we must not use the __constructor as a constructor for plugins
* because func_get_args ( void ) returns a copy of all passed arguments NOT references.
* This causes problems with cross-referencing necessary for the observer design pattern.
*
* @access protected
* @param object $subject The object to observe
* @param array $config An array that holds the plugin configuration
* @since 1.0
*/
function plgSystemAjaxchat(& $subject, $config)
{
parent::__construct($subject, $config);
$mainframe =& JFactory::getApplication();
$document =& JFactory::getDocument();
$doctype = $document->getType();
// deactivate for backend
if ($mainframe->isAdmin()) {
return false;
}
// add mootools 1.2
if ( $doctype == 'html' ) {
$document->addScript('components/com_ajaxchat/js/mootools-1.2-core.js');
$document->addScript('components/com_ajaxchat/js/mootools-1.2-more.js');
$document->addScript('components/com_ajaxchat/js/mootools-1.2-core-compat.js');
$document->addScript('components/com_ajaxchat/js/mootools-1.2-more-compat.js');
}
}
/**
* After Templte output is in buffer
*/
function onAfterRender() {
$mainframe =& JFactory::getApplication();
$document =& JFactory::getDocument();
$doctype = $document->getType();
// deactivate for backend
if ($mainframe->isAdmin()) {
return false;
}
// Only render for HTML output
if ( $doctype !== 'html' ) {
return;
}
// get the output buffer
$body = JResponse::getBody();
// remove mootools if not needed
if (stristr($body, 'mootools.js') || stristr($body, 'mootools-uncompressed.js')) {
$body = preg_replace("/<script.*?mootools(-uncompressed)?\.js.*?<\/script>/i", '', $body);
} else {
$body = preg_replace("/<script.*?mootools-1\.2\-.*?\.js.*?<\/script>[\s\t\r\n]*/i", "\n", $body);
}
JResponse::setBody($body);
}
}
?>