admin管理员组

文章数量:1290978

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

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
Share Improve this question edited Nov 22, 2014 at 10:19 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jan 4, 2013 at 2:58 rayray 411 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

I found a simple solution here and here:

unset($this->_scripts[JURI::root(true).'/media/system/js/caption.js']);

or

unset ( $this -> _metaTags[ 'http-equiv' ] ) ;
$this -> _scripts    = array ( ) ;
$this -> _script     = array ( ) ;

to get the whole lot. :)

Do not use:

$doc -> setHeadData($headData);

It does weird and mysterious things.

The problem that you (and I) were having is resetting the $doc variable through $doc->setHeadData($headData) instead of unsetting the _scripts and _script through $this. Coderwall. provides a thorough example.

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.

本文标签: jqueryHow to remove JavaScript code in the template header (Joomla 30x)Stack Overflow