0

Is it possible to add an external js file based on domain name from integration ?

Lets say the domain is: example.com. When user installs my integration, I want to add a myintegration.com/examplecom.js to my magento store.

Eligijus
  • 133
  • 5

2 Answers2

1

Use this in your default.xml:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <script src="http://myintegration.com/examplecom.js" src_type="url" />
    </head>
</page>
Manish Joy
  • 1,215
  • 12
  • 27
  • I don't have a default.xml and the script name should be generated with php depending on the domain name. I can't use php in xml. – Eligijus Jul 31 '17 at 10:38
0

I didn't try it but I think you can do this with require. In the requirejs-config.js of your module you can do something like:

var domain = window.location.hostname,  
    jsPath = 'myintegration.com/' + domain.match(/(?:http?:\/\/)?(?:www\.)?(.*?)\//) + '.js';

var config = {
    "paths": {
            "myintegration": jsPath
    }
};

(Indicative code, not tested)

nuovecode
  • 644
  • 5
  • 19