joomla仅在需要时在页面上加载mootools和/或jquery

问题描述:

I have a joomla 1.5 site where I use mootools (for chronoforms) and some custom jquery code.
In total there are only 2 pages on the whole site where I need mootools or jquery, but it both loads on all pages.

How can I make Joomla load the scripts only on the pages where needed?

Thanks

我有一个joomla 1.5网站,我使用mootools(用于chronoforms)和一些自定义jquery代码。
\ n整个网站上只有2个页面我需要mootools或jquery,但它们都加载到所有页面上。 p>

如何让Joomla仅在页面上加载脚本 需要吗? p>

谢谢 p> div>

You can modify joomla code :

go to :

libraries/joomla/html/html/behaviour.php

Search where the mootools is loaded .

Add an IF/ELSE :

ex:

$component = JRequest::getVar("option");
//xmap does not support Mootools 1.2
if($component == "com_xmap") {
   JHTML::script('jquery.js', 'media/system/js/', false);
 } 

I think you can put jQuery on and off by using these script to the specific page


$document = &JFactory::getDocument();
$document->addScript( '[path to jquery]' );
$noconflict = 'jQuery.noConflict();';
$document->addScriptDeclaration( $noconflict );

I'm not sure how to turn Mootool off. I afraid it always attach to the system. hope this help.

OK this article http://magazine.joomla.org/issues/Issue-Feb-2011/item/349-removing-mootools show how to remove mootool

You could use the following code in your template's index.php:

$app      =& JFactory::getApplication();

if ( $app->isSite() ) { // Make changes only on Front-end

    $frontpage = ( isset($this->_metaTags['standard']['title']) && $this->_metaTags['standard']['title'] == "Frontpage")? true : false;
    $com = JRequest::getVar("option");


    // When you need Mootools on frontpage only
    if(!$frontpage) {
       $prohibited = array (
           '/media/system/js/core.js',
           '/media/system/js/mootools-core.js',
           '/media/system/js/mootools-more.js',
           '/media/system/js/caption.js'
        );

 }

    // When you need jQuery, for example in "com_xmap" only
    if( $com != "com_xmap") {
        $prohibited = array (
           'http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js',
        );
     }

    foreach ($this->_scripts as $key => $value) {
        if( in_array($key, $prohibited ) ) {
            unset($this->_scripts[$key]);
        }
     }

}