10

I want to delete JCaption from head, jquery and jcaption file i deactivated with following methods:

unset($doc->_scripts[JURI::root(true) . '/media/jui/js/jquery.min.js']);
unset($doc->_scripts[JURI::root(true) . '/media/jui/js/jquery-noconflict.js']);
unset($doc->_scripts[JURI::root(true) . '/media/jui/js/jquery-migrate.min.js']);
unset($doc->_scripts[JURI::root(true) . '/media/system/js/caption.js']);

To delete jcaption on previous versions where mootools in using is it's needed:

$this->_script = preg_replace('%window\.addEvent\(\'load\',\s*function\(\)\s*{\s*new\s*JCaption\(\'img.caption\'\);\s*}\);\s*%', '', $this->_script);

I tried also so to delete it on jquery but i don't have with preg_replace() experience, what's to do to delete JCatpion from head?

My code:

$this->_script = preg_replace('%jQuery\(window\)\.on(\'load\',\s*function\(\)\s*{\s*new\s*JCaption\(\'img.caption\');\s*}\);\s*%', '', $this->_script);

5 Answers 5

5

This is a very old question and probably nobody reads it anymore, but anyways I will post my answer.

In the regex for the JQuery caption you have errors. You don't need to escape dots but you have to escape special characters. And also you need to target the correct key in the array so you need to add ['text/javascript'] after calling the script object.

So the correct regex/preg_replace would be:

$this->_script['text/javascript'] = preg_replace('%jQuery\(window\).on\(\'load\',\s*function\(\)\s*\{\s*new\s*JCaption\(\'img.caption\'\)\;\s*\}\)\;\s*%', '', $this->_script['text/javascript']);

Not so long ago I was also struggling with regex but this site helped a lot: http://regexr.com/ If you trying out the site note that you don't need to use the open and close tags because its already there automatically.

1
  • To avoid escaping single quotes, wrap the pattern in double quotes. Semicolons don't need to be escaped. While not escaping dots will still deliver the expected outcome in this case, it is more accurate/efficient to match literal dots rather than an "any character" metacharacter in the pattern, so I recommend escaping the dots. Nov 27, 2018 at 20:07
4

Try this:

$this->_scripts = array();
unset($this->_script['text/javascript']);
4
  • This is GREAT! I got rid of all of those unset($doc->_scripts lines of code and replace with 2.
    – mmundiff
    Sep 14, 2015 at 18:38
  • Feels good to know that i was a help for you :) Sep 14, 2015 at 18:42
  • Hopefully it helped the original poster and they accept yours as the answer! Thanks Again
    – mmundiff
    Sep 14, 2015 at 18:51
  • Please always accompany snippets with a plain English explanation. Sep 21, 2020 at 9:52
4

A better solution for disabling JCaption injection in content pages is to override Joomla's core 'behavior.caption' HTML behavior with a stub function. You achieve this by creating a simple system plugin as follows:

use Joomla\CMS\HTML\HTMLHelper;

class PlgSystemCaptionDisabler extends JPlugin
{
    function onAfterInitialise() {
        if (!HTMLHelper::isRegistered('behavior.caption')) {
            HTMLHelper::register('behavior.caption', array(__CLASS__, 'caption'));
        }
    }

    // Dummy override function to disable default Joomla 'caption' behavior
    public static function caption() {}
}
1
1

Try this:

if (isset($this->_script['text/javascript']))
{
    $this->_script['text/javascript'] = preg_replace('%window\.addEvent\    (\'load\',\s*function\(\)\s*{\s*new\s*JCaption\(\'img.caption\'\);\s*}\);\s*%',     '', $this->_script['text/javascript']);
    if (empty($this->_script['text/javascript']))
        unset($this->_script['text/javascript']);
}
$this->_scripts = array();
1
  • Please always accompany snippets with a plain English explanation. Sep 21, 2020 at 9:51
0

If you are willing to use a plugin to unset the script tag injected, the following can be useful.

public function onBeforeRender() {
    // Front end
    if ($this->app instanceof JApplicationSite) {
        $doc = JFactory::getDocument();

        foreach ($doc->_script as $key => $script) {
            if (stristr($key, 'text/javascript') !== false) {
                unset($doc->_script['text/javascript']);
            }
        }
    }
}
1
  • Not only does the php manual declare that strstr() is less efficient than strpos() when checking for the existence of a substringin a string, this task doesn't need to search for a substring -- $key is the full string match. Nov 27, 2018 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.