添加远程 javascript 到 prestashop 1.7
Add remote javascript to prestashop 1.7
我在使用 prestashop 1.7 时遇到以下问题
- 我用钩子注册静态javascript
actionFrontControllerSetMedia
- 我尝试使用相同的方法注册动态远程加载 javascript:
- 当然它不起作用,因为
registerJavascript
需要本地路径。
Context->addJs()
不再适合我了。
有什么办法可以把javascript加到文档中吗?
在你的任意一个JS文件中添加以下代码,它将加载远程JS文件:
function loadScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://my.remote.js';
document.body.appendChild(script);
}
参见: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]
);
似乎它已在 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']
);
如果您需要为所有页面或特定页面添加 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
....
我在使用 prestashop 1.7 时遇到以下问题
- 我用钩子注册静态javascript
actionFrontControllerSetMedia
- 我尝试使用相同的方法注册动态远程加载 javascript:
- 当然它不起作用,因为
registerJavascript
需要本地路径。 Context->addJs()
不再适合我了。
- 当然它不起作用,因为
有什么办法可以把javascript加到文档中吗?
在你的任意一个JS文件中添加以下代码,它将加载远程JS文件:
function loadScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://my.remote.js';
document.body.appendChild(script);
}
参见: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]
);
似乎它已在 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']
);
如果您需要为所有页面或特定页面添加 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
....