headerbanner

Removing javascript from head without hack - Joomla

Joomla adds a javascript code to the head of every page to initialise image captions. Here is an option to remove it.

<?php
$headerstuff = $this->getHeadData();
foreach ($headerstuff as $key => $value){
  foreach ($value as $keyx => $valuex){
    if (($key == "links")and (strpos($keyx,"feed") !== false)) {
      unset ($headerstuff[$key][$keyx]);
    }
    if (($key == "scripts") and (strpos($keyx,"caption") !== false)) {
      unset ($headerstuff[$key][$keyx]);
    }
    if (($key == "script") and (strpos($keyx,"text/javascript") !== false)) {
      if (strpos($headerstuff[$key][$keyx],"caption") !== false) {
        $headerstuff[$key][$keyx] = preg_replace("~\s*window\.addEvent\s*\(\s*\'load\'\s*,\s*function\s*\(\s*\)\s*\{\s*new\s*JCaption[^;]*;[^;]*;\s*~m", '', $headerstuff[$key][$keyx]);        
 
      }
    }
  }                                 
}
$this->setHeadData($headerstuff);
if (empty($this->_script['text/javascript'])){
    unset($this->_script['text/javascript']);
}
?>

Add the foregoing code in your template index.php file, before a line with code :

<jdoc:include type="head" />

You can remove an rss link or the other lines from head too. Add the following code in index.php file to see what is incluled in "include type='head'".

<?php
$headerstuff = $this->getHeadData();
foreach ($headerstuff as $key => $value){
    echo  "\n\n\n firstkey: " .$key;
    echo "\n firstvalue:" .$value;
      foreach ($value as $keyx => $valuex){
        echo "\n\t seckey: " .$keyx;
        echo "\n\t secvalue:" .$valuex;
        foreach ($valuex as $keyy => $valuey){
               echo "\n\t\t thirdkey: " .$keyy;
               echo "\n\t\t thirdvalue: " .$valuey;
        }
      }                                 
}
?>

Look at the source code of your page:

Firstkey: title
 firstvalue:Dobře nalezitelné weby, PR, komunikace - Home


 Firstkey: description
 firstvalue:public relations, dobře nalezitelné weby


 Firstkey: link
 firstvalue:


 Firstkey: metaTags
 firstvalue:Array
	 seckey: http-equiv
	 secvalue:Array
		 thirdkey: content-type
		 thirdvalue: text/html
	 seckey: standard
	 secvalue:Array
		 thirdkey: keywords
		 thirdvalue: 
		 thirdkey: rights
		 thirdvalue: 


 Firstkey: links
 firstvalue:Array
	 seckey: /?format=feed&type=rss
	 secvalue:Array
		 thirdkey: relation
		 thirdvalue: alternate
		 thirdkey: relType
		 thirdvalue: rel
		 thirdkey: attribs
		 thirdvalue: Array
	 seckey: /?format=feed&type=atom
	 secvalue:Array
		 thirdkey: relation
		 thirdvalue: alternate
		 thirdkey: relType
		 thirdvalue: rel
		 thirdkey: attribs
		 thirdvalue: Array


 Firstkey: styleSheets
 firstvalue:Array
	 seckey: /components/com_jcomments/tpl/default/style.css?v=21
	 secvalue:Array
		 thirdkey: mime
		 thirdvalue: text/css
		 thirdkey: media
		 thirdvalue: 
		 thirdkey: attribs
		 thirdvalue: Array


 Firstkey: style
 firstvalue:Array


 Firstkey: scripts
 firstvalue:Array
	 seckey: /components/com_jcomments/js/jcomments-v2.3.js?v=8
	 secvalue:Array
		 thirdkey: mime
		 thirdvalue: text/javascript
		 thirdkey: defer
		 thirdvalue: 
		 thirdkey: async
		 thirdvalue: 
	 seckey: /components/com_jcomments/libraries/joomlatune/ajax.js?v=4
	 secvalue:Array
		 thirdkey: mime
		 thirdvalue: text/javascript
		 thirdkey: defer
		 thirdvalue: 
		 thirdkey: async
		 thirdvalue: 
	 seckey: /media/system/js/mootools-core.js
	 secvalue:Array
		 thirdkey: mime
		 thirdvalue: text/javascript
		 thirdkey: defer
		 thirdvalue: 
		 thirdkey: async
		 thirdvalue: 
	 seckey: /media/system/js/core.js
	 secvalue:Array
		 thirdkey: mime
		 thirdvalue: text/javascript
		 thirdkey: defer
		 thirdvalue: 
		 thirdkey: async
		 thirdvalue: 
	 seckey: /media/system/js/caption.js
	 secvalue:Array
		 thirdkey: mime
		 thirdvalue: text/javascript
		 thirdkey: defer
		 thirdvalue: 
		 thirdkey: async
		 thirdvalue: 
	 seckey: /media/system/js/mootools-more.js
	 secvalue:Array
		 thirdkey: mime
		 thirdvalue: text/javascript
		 thirdkey: defer
		 thirdvalue: 
		 thirdkey: async
		 thirdvalue: 


 Firstkey: script
 firstvalue:Array
	 seckey: text/javascript
	 secvalue:do_nothing( function() {
				
			});


 Firstkey: custom
 firstvalue:Array

Now you can see how to remove the other elements from head section, for example the RSS links.

<?php
$headerstuff = $this->getHeadData();
foreach ($headerstuff as $key => $value){
  foreach ($value as $keyx => $valuex){
    //no RSS link
    if ($key == "links") {
      unset ($headerstuff[$key][$keyx]);
    }
  }                                 
}
$this->setHeadData($headerstuff);
?>
{jcomments on}