移除Joomla的mootools-core.js和JCaption

By | 2013 年 6 月 24 日

偶看在使用Joomla 3.1建站的时候发现,Joomla会在网页head区域自动添加mootools-core.js等JS文件,还会添加JCaption,完整的代码为

[php]
<script src="/media/system/js/mootools-core.js" type="text/javascript"></script>
<script src="/media/system/js/core.js" type="text/javascript"></script>
<script src="/media/system/js/caption.js" type="text/javascript"></script>
<script type="text/javascript">
window.addEvent(‘load’, function() {
new JCaption(‘img.caption’);
});
</script>
[/php]

对于偶看来说,这些代码都用不上,而且这些代码体积还不算小,通过查看“Mootools Enabler/Disabler”插件的源代码,偶看找到了去除JS的核心代码,将以下代码添加到模板文件夹下的 index.php 的头部

[php]
<?php
$app = JFactory::getApplication();
$doc = JFactory::getDocument();
$pageParams = $app->getParams();
unset($doc->_scripts[JURI::root(true) . ‘/media/system/js/mootools-core.js’]);
unset($doc->_scripts[JURI::root(true) . ‘/media/system/js/mootools-more.js’]);
unset($doc->_scripts[JURI::root(true) . ‘/media/system/js/core.js’]);
unset($doc->_scripts[JURI::root(true) . ‘/media/system/js/caption.js’]);
unset($doc->_scripts[JURI::root(true) . ‘/media/system/js/modal.js’]);
unset($doc->_scripts[JURI::root(true) . ‘/media/system/js/mootools.js’]);
unset($doc->_scripts[JURI::root(true) . ‘/plugins/system/mtupgrade/mootools.js’]);
?>
[/php]

删除了JS文件还不够,还需要删除head代码,否则,浏览器会报错。

[php]
window.addEvent(‘load’, function() {
new JCaption(‘img.caption’);
});
[/php]

删除以上代码的解决方案来自:Removing JCaption in Joomla!2.5,对于Joomla 3.1也是同样适用的。

还是在模板文件夹下的 index.php 的头部,添加正则表达式

[php]
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’]);
}
[/php]

即可将JCaption代码删除,那么最后完整版本为:

[php]
<?php
$app = JFactory::getApplication();
$doc = JFactory::getDocument();
$pageParams = $app->getParams();
unset($doc->_scripts[JURI::root(true) . ‘/media/system/js/mootools-core.js’]);
unset($doc->_scripts[JURI::root(true) . ‘/media/system/js/mootools-more.js’]);
unset($doc->_scripts[JURI::root(true) . ‘/media/system/js/core.js’]);
unset($doc->_scripts[JURI::root(true) . ‘/media/system/js/caption.js’]);
unset($doc->_scripts[JURI::root(true) . ‘/media/system/js/modal.js’]);
unset($doc->_scripts[JURI::root(true) . ‘/media/system/js/mootools.js’]);
unset($doc->_scripts[JURI::root(true) . ‘/plugins/system/mtupgrade/mootools.js’]);
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’]);
}
?>
[/php]

2 thoughts on “移除Joomla的mootools-core.js和JCaption

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据