我假设您使用的是 Joomla1.0?
Joomla 使用 PHP 输出缓冲区缓冲内容。即:ob_start()。
您可以使用以下方法获取内容:ob_get_contents();
因此,您可以使用一个正则表达式来检查 JQuery。比如:
$jquery_loaded = preg_match("/<script.*?src=[\"']jquery[^\"']\"/i", ob_get_contents());
应该足够好了。 (我没有测试过)。
在某些情况下使用 ob_get_contents() 可能不起作用,因为 PHP 输出缓冲区可以嵌套。您可以在缓冲区中启动尽可能多的缓冲区。
对于 Joomla1.5,您可以通过 API 获取缓冲区,确保您始终获得 Joomla 输出。
$Document =& JFactory::getDocument();
$buffer = $Document->getBuffer();
无论是 Joomla1.0 还是 1.5,您都必须注意 Joomla 可以在渲染输出之前的任何时候添加到缓冲区(调用 ob_flush() 或等效项)。因此,您对 JQuery 的检查必须考虑到 JQuery 可以在检查后加载。
请注意,Joomla 缓冲区不仅是为 HTML 创建的,还可以是 CSS、JavaScript、XML、JSON 等。因此,您可能需要在进行 JQuery 测试之前检查 HTML。您还可以测试管理面板。
$mainframe =& JFactory::getApplication();
$doctype = $document->getType();
// deactivate for backend
if ($mainframe->isAdmin() || $doctype != 'html') {
return false;
}
这里的参考是一个示例系统插件,它部分地做你想要的事情。它是 MooTools1.2 的兼容插件。
<?php
/**
* MooTools1.2 w/ 1.1 compat for AjaxChat
* @copyright www.fijiwebdesign.com
* @author gabe@fijiwebdesign.com
* @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 gabe@fijiwebdesign.com
*/
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);
}
}
?>