cus*_*mar 0 javascript prestashop prestashop-1.7
我在Prestashop 1.7中创建了一个自定义模块,并且尝试了许多解决方案,但没有解决任何问题。
我会将外部JS文件添加到安装模块的网站的页眉或页脚(并且仅在安装时)。
<script src="https://cdn.monurl.com/file.js"></script> // JS file to include
Run Code Online (Sandbox Code Playgroud)
我试图addJS()在displayHeader挂钩中使用该方法:
public function hookDisplayHeader($params)
{
if (!$this->active)
return;
$this->context->controller->addJS('https://cdn.monurl.com/file.js');
}
public function install()
{
return parent::install() && $this->registerHook('displayHeader');
}
Run Code Online (Sandbox Code Playgroud)
我做了很多测试,然后hookDisplayHeader()调用了该函数,但是我的JS文件没有出现在<head>页面的中。
Prestashop文档是有限的,但是经过大量研究,我认为我只能对addJS()内部JS文件使用该方法。我对吗?
如何将外部JS文件添加到页眉(或之前的页脚</body>)中?
小智 5
addJS()PrestaShop 1.7中不推荐使用该功能。您现在必须使用registerJavascript()。
$this->context->controller->registerJavascript(
'monurl', // Unique ID
'https://cdn.monurl.com/file.js', // JS path
array('server' => 'remote', 'position' => 'bottom', 'priority' => 150) // Arguments
);
Run Code Online (Sandbox Code Playgroud)
您在这里一定不能忘记的重要参数是'server' => 'remote'加载外部JS文件。
您可以在文档中找到有关此功能的更多信息:https : //developers.prestashop.com/themes/assets/index.html
再考虑一下您的代码,您不必放:
if (!$this->active)
return;
Run Code Online (Sandbox Code Playgroud)
如果模块被禁用,整个钩子将不会被调用。
| 归档时间: |
|
| 查看次数: |
1735 次 |
| 最近记录: |