gpt4 book ai didi

javascript - 将外部 JS 文件链接到 Prestashop

转载 作者:行者123 更新时间:2023-11-28 12:15:30 26 4
gpt4 key购买 nike

我正在 Prestashop 1.7 中创建一个自定义模块,我尝试了很多解决方案,但没有解决我的问题。

我会将外部 JS 文件添加到安装模块的网站的页眉或页脚(并且仅在安装模块时)。

<script src="https://cdn.monurl.com/file.js"></script> // JS file to include

我尝试使用 addJS() displayHeader Hook 中的方法:

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');
}

我做了很多测试,hookDisplayHeader()函数被调用,但是我的JS文件没有出现在<head>中我的页面。

Prestashop的文档有限,但是经过多次研究,我想我只能使用addJS()方法与内部JS文件。我说得对吗?

我应该如何将外部 JS 文件添加到我的页眉(或 </body> 之前的页脚)?

最佳答案

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
);

这里您不能忘记的重要参数是 'server' => 'remote' 来加载外部 JS 文件。

您可以在文档中找到有关此功能的更多信息:https://developers.prestashop.com/themes/assets/index.html

再想想你的代码,你不必把:

if (!$this->active)
return;

如果模块被禁用,整个钩子(Hook)将不会被调用。

关于javascript - 将外部 JS 文件链接到 Prestashop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50567458/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com