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

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.

  • 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. – mickmackusa Nov 27 '18 at 20:07
4

Try this:

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

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();
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']);
            }
        }
    }
}
  • 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. – mickmackusa Nov 27 '18 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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