Joomla 3.X - Remove tooltip script from header?

2019-02-16 21:17发布

I'm trying to remove unwanted scripts from my custom joomla template header, which I've managed to remove everything except for this:

<script type="text/javascript">
jQuery(document).ready(function()
                {
                    jQuery('.hasTooltip').tooltip({});
                });
  </script>

I've searched for hours and I've tried numerous things to get it removed, but I can't seem to get rid of it. Here's what I've done to remove the other scripts I don't want (for anyone else who has this issue):

# unset frameworks
JHtml::_('bootstrap.framework',false);
JHtml::_('jquery.framework',false);

# unset scripts
unset($doc->_scripts[$this->baseurl.'/media/jui/js/jquery.min.js']);
unset($doc->_scripts[$this->baseurl.'/media/jui/js/jquery-noconflict.js']);
unset($doc->_scripts[$this->baseurl.'/media/jui/js/bootstrap.min.js']);

If someone could help me remove that tooltip javascript, that would be fantastic. Oh and I don't want to touch the core files, so I'm trying to remove it from the template index.php file itself.

9条回答
Fickle 薄情
2楼-- · 2019-02-16 21:59

Based on Joomla documents, 2 things to consider:

a) In the below code, you're actually enabling/including bootstrap and jquery:

# unset frameworks
JHtml::_('bootstrap.framework',false);// including bootstrap!
JHtml::_('jquery.framework',false);// including jquery!

When bootstrap is enabled, joomla automatically enables jquery, and if joomla enabled jquery, joomla automatically enables tooltip.

Just don't call these functions. The unset you used will remove bootstrap and jquery correctly:

unset($doc->_scripts[$this->baseurl.'/media/jui/js/jquery.min.js']);
unset($doc->_scripts[$this->baseurl.'/media/jui/js/jquery-noconflict.js']);
unset($doc->_scripts[$this->baseurl.'/media/jui/js/bootstrap.min.js']);
unset($doc->_scripts[$this->baseurl.'/media/jui/js/jquery-migrate.min.js']);

b) if the tooltip script is still included, it's probably inserted by JHtml::_('behavior.tooltip'); somewhere in the used component.

Lastly, never never never modify Joomla core files. it's a worst-practice.

PS. For those who mentioned that the tooltip script is inserted but they don't find it in the $doc, that's because the $doc doesn't contain inline scripts.

查看更多
叼着烟拽天下
3楼-- · 2019-02-16 21:59

I have already meet this issue, I am using Joomla 3. If it is your case then you can solve it by doing this : Joomla 3 comes with jQuery on board, so by adding Joomla yourself, this may generate the issue. Also make sure you include your jQuery over <jdoc:include type="head" />. If you necessary wants to include it. But I do not recommend this. Hope this helps

查看更多
趁早两清
4楼-- · 2019-02-16 22:02

I had the same problem when I was building a Joomla template/site with only HTML5, CSS3 and some small jQuery plugins for effects. It was unworthy of including heavy Bootstrap just to show some tooltips which I also didn't use at all.

Althought I already unset media/jui/js/bootstrap.min.js from JDocument but these lines of code

jQuery(document).ready(function(){
    jQuery('.hasTooltip').tooltip({"html": true,"container": "body"});
});

were still appended by libraries/cms/html/bootstrap.php. So I got error "function tooltip not found".

I solved that by adding an empty function to my template's JS file.

jQuery.fn.tooltip = function (option) { };
查看更多
爷的心禁止访问
5楼-- · 2019-02-16 22:04

You'll have to manually parse $doc->_script. The $doc->_scripts array contains scripts that are linked to another source while $doc->_script is for script declarations like the tooltip one.

查看更多
女痞
6楼-- · 2019-02-16 22:08

Create a system plugin with the follow code.

The First foreach loop unsets the .js file(s) added to the head. And the next foreach loop unsets the js code injected inside <script> tags.

There are two separate properties containing the scripts the _script & _scripts

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

        $search = array(
//                'jquery',
//                'caption.js',
//                'bootstrap.min.js',
//                'core.js',
//                'keepalive.js',
//                'punycode.js',
//                'validate.js',
//                'calendar.js',
//                'calendar-setup.js',
//                'mootools-more.js',
//                'modal.js',
//                'ajax-chosen.min.js',
//                'tabs-state.js',
//                'frontediting.js',
//                'html5fallback.js',
//                'jui/js/bootstrap.min.js',
//                'jquery.min.js',
            'jui/js/',
            'system/js/',
//                'text/javascript'
        );
        foreach ($doc->_scripts as $key => $script) {
            foreach ($search as $findme) {
                if (stristr($key, $findme) !== false) {
                    unset($doc->_scripts[$key]);
                }
            }
        }

        foreach ($doc->_script as $key => $script) {
            if (stristr($key, 'text/javascript') !== false) {
                unset($doc->_script[$key]);
            }
        }

    }
}
查看更多
神经病院院长
7楼-- · 2019-02-16 22:09

I managed to get rid of the embedded javascript using this code in my template's index.php file:

unset($this->_script['text/javascript']);

disclaimer: I am not a PHP developer, so use above code at your own risk :)

查看更多
登录 后发表回答