【问题标题】:Check if jQuery is included in Header (Joomla)检查 jQuery 是否包含在 Header (Joomla) 中
【发布时间】:2010-11-22 20:25:16
【问题描述】:

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

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

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

这是使用函数:

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

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

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

【问题讨论】:

  • 您可能希望将此问题改写为“检查 jQuery 是否包含在 Joomla 的标头中”。

标签: php jquery html plugins joomla


【解决方案1】:

更好的是,您可以使用 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); 
}

【讨论】:

  • 但是如果其他写得不好的模块加载jQuery,这并不能解决问题。
  • 这没有回答问题。
【解决方案2】:

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

}

?>

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:

      这是不可能的。 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 库的异步性。似乎最好的方法是使用附加到脚本包含的onload 事件处理程序,但 DOM 不支持。

      编辑 2:好的,现在我认为您最好的选择是将其简单地包含在页面中一次,每页仅使用一个脚本标签。您可以通过将其添加到某种全局标题中然后在其他任何地方将其删除来做到这一点。然后,无论如何,不​​要再次包含它。这可能意味着您将它加载到一些不需要它的页面上,但由于现代浏览器上的缓存,它应该不是问题。

      【讨论】:

        【解决方案5】:

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

        $document =& JFactory::getDocument();
        

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

        在那个版本中,您实际上是在去除多余的 jQuery 负载。

        【讨论】:

        • 我不确定,因为我是 Joomla 开发的新手,但我尝试 print_r getHeadData() 并且我发现它只存在插件添加的脚本,而不是插件添加的脚本模块。我认为由于它们是在之后加载的,因此它们只是绕过 joomla 直接在 html 上打印它们的脚本标签。如果我错了,请告诉我。
        【解决方案6】:

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

        【讨论】:

          猜你喜欢
          • 2013-11-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-26
          • 2019-01-28
          • 2014-02-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多