我对PHP和Joomla有一个问题。
当我用SEO工具测试我的网站时,有一条关于2块脚本的消息。
“您的页面有2块脚本资源和3块CSS资源。这会导致呈现页面的延迟。”
因此,我尝试异步资源来打开呈现我的站点的障碍。
首先是$file = 'https://www.google.com/recaptcha/api/js/recaptcha_ajax.js';
和第二$file = 'https://www.google.com/recaptcha/api.jsonload=JoomlaInitReCaptcha2&render=explicit&hl=' . JFactory::getLanguage()->getTag();
下面是我尝试过的代码,有人能帮助我正确地使用异步吗?
public function onInit($id = 'dynamic_recaptcha_1')
{
$pubkey = $this->params->get('public_key', '');
if ($pubkey === '')
{
throw new Exception(JText::_('PLG_RECAPTCHA_ERROR_NO_PUBLIC_KEY'));
}
if ($this->params->get('version', '1.0') === '1.0')
{
JHtml::_('jquery.framework');
$theme = $this->params->get('theme', 'clean');
$file = 'https://www.google.com/recaptcha/api/js/recaptcha_ajax.js';
JHtml::_('script', $file);
JFactory::getDocument()->addScriptDeclaration('jQuery( document ).ready(function()
{Recaptcha.create("' . $pubkey . '", "' . $id . '", {theme: "' . $theme . '",' . $this->_getLanguage() . 'tabindex: 0});});');
}
else
{
// Load callback first for browser compatibility
JHtml::_('script', 'plg_captcha_recaptcha/recaptcha.min.js', array('version' => 'auto', 'relative' => true));
$file = 'https://www.google.com/recaptcha/api.js?onload=JoomlaInitReCaptcha2&render=explicit&hl=' . JFactory::getLanguage()->getTag();
JHtml::_('script', $file);
}
return true;
}发布于 2018-10-18 15:58:16
您所需要做的就是更改以下一行:
JHtml::_('script', 'plg_captcha_recaptcha/recaptcha.min.js', array('version' => 'auto', 'relative' => true)); 至:
JHtml::_('script', 'plg_captcha_recaptcha/recaptcha.min.js', array('version' => 'auto', 'relative' => true), array('async'=>'async')); 有关如何在Joomla上异步加载JS文件的指南,请参阅这个职位。
https://stackoverflow.com/questions/52518000
复制相似问题