Search code examples
javascriptjquerycssjoomlamootools

How to remove JavaScript code in the template header (Joomla! 3.0.x)?


I have created a template in Joomla! 3x, but I want to remove the default JavaScript code and stylesheets in the page header.

My code is:

$doc = JFactory::getDocument();

//Remove default configuration
$doc->setGenerator("");
$headData = $doc -> getHeadData();
unset($headData['metaTags']['http-equiv']);
$doc -> setHeadData($headData);   //Is OK
$doc -> _styleSheets = array();   //Is OK
$doc -> _scripts = array();       //Is OK
$doc -> _script =  array();  **// Not OK**

//Add foundation
$doc->addStyleSheet('templates/'.$this->template.'/css/foundation.css');
$doc->addScript('templates/'.$this->template.'/js/jquery.js');
$doc->addScript('templates/'.$this->template.'/js/modernizr.foundation.js');

The template header HTML is now:

function keepAlive() { var myAjax = new Request({method: "get", url: "index.php"}).send();} window.addEvent("domready", function(){ keepAlive.periodical(840000); });
jQuery(document).ready(function() {
    jQuery('.hasTooltip').tooltip({});
});
// The block is not removed

How do I remove this JavaScript code?

Reference files:

  • /libraries/joomla/html/behavior.php
  • /libraries/cms/html/bootstrap.php

Solution

  • Use JHtml::_('behavior.disable','behavior'); to remove specific behavior

    We have so many posts on the web and forums where users are asking to remove this or that behavior. Instead of them hacking their way trough the views and core we could have:

    JHtml::_('behavior.disable','specific behavior'); to remove the ones we don't want

    Example scenario

    Tooltips are loaded no matter if your HTML overrides are not loading it.

    JHtml::_('behavior.disable','tooltips'); placed in template index.php could do this for you and this way help you clean your head tag from unwanted scripts.

    In 3.0 if you just want to remove tooltips from registration you have to copy the view to HTML override. Remove JHtml::_('behavior.tooltip'); from the file and then it is gone. I think that this should be done without HTML overrides.

    And in 2.5.x even using HTML overrides does not help. Tooltip stays.

    If any other extension that you have installed is loading behavior that you don't want, it will take you longer to figure out where it is.