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.


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">
        <script src="http://myintegration.com/examplecom.js" src_type="url" />
  • 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

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.