我对 prestashop 1.7 有以下问题
- 我用钩子注册静态javascript
actionFrontControllerSetMedia
- 我尝试使用相同的方法注册动态远程加载的 javascript:
- 当然,它不像
registerJavascript
预期的本地路径那样工作。 Context->addJs()
不再适合我了。
- 当然,它不像
有什么解决方案如何将javascript添加到文档中?
我对 prestashop 1.7 有以下问题
actionFrontControllerSetMedia
registerJavascript
预期的本地路径那样工作。Context->addJs()
不再适合我了。有什么解决方案如何将javascript添加到文档中?
如果您需要为所有页面或特定页面添加 JavaScript/CSS,您可以使用 theme.yml 来更新它。例如,如果您需要在所有页面上添加以下两个外部 CSS。在你的 theme.yml 中添加以下内容:
....
css:
all:
- id: fontawesome-css
path: https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css
media: all
priority: 200
inline: false
server: remote
- id: googlecss
path: https://fonts.googleapis.com/css?family=Open+Sans:300,400,500,600,700,800
media: all
priority: 200
inline: false
server: remote
....
见:https ://github.com/PrestaShop/PrestaShop/pull/7022 (1.7.0.2引入)
$this->registerJavascript(
'remote-bootstrap',
'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js',
['server' => 'remote', 'position' => 'head', 'priority' => 20]
);
在您的任何 JS 文件中添加以下代码,它将加载远程 JS 文件:
function loadScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://my.remote.js';
document.body.appendChild(script);
}
似乎它已在1.7.0.2 https://github.com/PrestaShop/PrestaShop/pull/7022中重新引入, 他们将使用registerJavascript和registerStylesheet代替。
$this->registerJavascript('remote-bootstrap-head', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js', ['server' => 'remote', 'position' => 'head', 'priority' => 20]);
$this->registerJavascript('remote-bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js', ['server' => 'remote', 'position' => 'bottom', 'priority' => 20]);
$this->registerStylesheet('remote-bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css', ['server' => 'remote', 'priority' => 20]);
$this->context->controller->registerJavascript(
'remote-openpay-js',
'https://openpay.s3.amazonaws.com/openpay.v1.min.js',
['position' => 'bottom', 'server' => 'remote']
);