首页 > 解决方案 > 将自定义 js 添加到特定的 prestashop 页面

问题描述

我正在使用 Prestashop v1.6。

当我导航到此 url 路线时:

localhost/dumb/url

我想添加这个脚本

src="localhost/dumb/url/script.js",

我只需要在该 URL 上公开该脚本。

我想要什么:一步一步的指南来了解我应该做什么

标签: javascriptphpprestashopprestashop-1.6prestashop-modules

解决方案


Standard way:

Your URL (localhost/dumb/url) must have a controller created by your module. you can add your js file using this controller

$this->addJs(__PS_BASE_URI__.'/dumb/url/script.js');

Not recommended way:

Add this code to FrontController.php (or override this controller):

if (pathinfo($_SERVER['REQUEST_URI'])['dirname'] == '/dumb/url') {
    $this->addJs(__PS_BASE_URI__.'/dumb/url/script.js');
}

推荐阅读