jPO
jPO

Reputation: 2552

Add remote javascript to prestashop 1.7

I am having following issue with prestashop 1.7

  1. I register static javascript with the hook actionFrontControllerSetMedia
  2. I try to register dynamic remotely loaded javascript with the same method:
    • Of course it doesn't work as the registerJavascript expects local path.
    • Context->addJs() doesn't work for me anymore.

Is there any solution how to add javascript to the document?

Upvotes: 2

Views: 4449

Answers (5)

Neeraj Gulia
Neeraj Gulia

Reputation: 680

In case you need to add the JavaScript/CSS for all pages or for a specific page, you can use theme.yml to update it. e.g. if you need to add following two external css on all of your pages. Add following in your 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
....

Upvotes: 2

Federico Balderas
Federico Balderas

Reputation: 172

$this->context->controller->registerJavascript(
    'remote-openpay-js',
     'https://openpay.s3.amazonaws.com/openpay.v1.min.js',
     ['position' => 'bottom', 'server' => 'remote']
);

Upvotes: -1

katopz
katopz

Reputation: 671

Seem like it has been reintroduced in 1.7.0.2 https://github.com/PrestaShop/PrestaShop/pull/7022 They will use registerJavascript and registerStylesheet instead.

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

Upvotes: 0

aleeks
aleeks

Reputation: 66

See: https://github.com/PrestaShop/PrestaShop/pull/7022 (introduced in 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]
);

Upvotes: 2

Knowband Plugins
Knowband Plugins

Reputation: 1317

Add the following code in any of your JS file, it will load the remote JS file:

function loadScript() {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'http://my.remote.js';
    document.body.appendChild(script);
}

Upvotes: 0

Related Questions