检查jader是否包含在Header(Joomla)中

时间:2009-08-26 01:56:02

标签: php jquery html plugins joomla

有没有办法检查jQuery是否使用PHP加载?

我在Joomla中有两个不同的插件来加载jQuery JS,但是当它被多次包含时它无法正常工作。

更多地解释一下这个过程: Joomla提供了在呈现HTML源之前拦截它的能力,主要是处理源代码本身。

这是使用函数:

onPrepareContent(&$row, &$params, $limitstart)

$ row是可以解析的页面的HTML内容。

我在想,也许preg_match可以运作,但没有太多经验。

6 个答案:

答案 0 :(得分:14)

更好的是,您可以使用JavaScript验证它,如果遗漏则将其添加到头部。

   if (typeof jQuery == 'undefined') { 
   var head = document.getElementsByTagName("head")[0];
   script = document.createElement('script');
   script.id = 'jQuery';
   script.type = 'text/javascript';
   script.src = 'js/jquery.js';
   head.appendChild(script); 
}

答案 1 :(得分:3)

我假设您正在使用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);
    }

}

?>

答案 2 :(得分:1)

首先应该在javascript中进行检查。例如:

window.onload = function()
{
   if (typeof(window.jQuery)=="undefined")
      alert('jQuery no load');
   else
      alert('jQuery Ok');
}

然后,您可以通过ajax将消息发送到服务器。

答案 3 :(得分:1)

那是不可能的。 PHP是服务器端,而Javascript(编写JQuery语言)是客户端。 PHP代码本身必须在包含Javascript的文本之前运行,甚至可以将其发送给用户并尝试加载。

你最好的选择是不要多次包含jQuery。您可以通过将所有包含替换为以下内容来完成此操作:

<script type='text/javascript'>
    if($) { } // test to see if the jQuery function is defined
    else document.write("<script type='text/javascript' src='jquery.js'></script>");
</script>

不可否认,这有点像黑客,可能有更好的方法。

编辑:经过一番思考:由于从服务器加载jQuery库的异步性,这可能甚至不起作用。似乎最好的方法是使用附加到脚本include的onload事件处理程序,但DOM不支持它。

编辑2:好的,现在我认为你最好的选择就是在页面中只使用一个脚本标记将其包含一次。您可以通过将其添加到某种类型的全局标头然后在其他地方将其删除来完成此操作。然后,无论如何,不​​要再包括它。这可能意味着您将它加载到一些不需要它的页面上,但由于现代浏览器上的缓存,它应该不是问题。

答案 4 :(得分:1)

您可能想尝试使用JDocument对象来获取标头,搜索jQuery,然后根据需要重置。 getHeadData()setHeadData()方法可以帮助您。您可以使用以下代码获取当前JDocument对象:

$document =& JFactory::getDocument();

您可能还想查看对类似问题的回复:Joomla, jQuery modules conflicting

在那个实际上,你实际上正在剥离多余的jQuery负载。

答案 5 :(得分:0)

除非Joomla提供一种告诉你它包含哪些脚本的方法? (Zend Framework有这样一个列表)

相关问题