1

I want to add an external script to my store. I have added the script in my phtml file and it is working fine. But I want to follow the magento EQP validation. Here is my working code my layout xml

<head>        
  <script type="text/js" src="https://test.test1.com/js/v1/test.js" src_type="url" />
</head>

This is my phtml file:

<div><?php echo $block->getHtmlSnippet();  ?></div>
<div class="test-wrap">
    <div class="test-inner-wrap">        
        <script>
            Test.test1({              // Test.test1 is the function definted in https://test.test1.com/js/v1/test.js
                token: '<?php //echo $block->getToken();  ?>'
            });
        </script>
    </div>
</div>

Now I have changed it as follows (using require js function)

Phtml file

<div><?php echo $block->getHtmlSnippet();  ?></div>
<div class="test-wrap">
    <div class="test-inner-wrap">          
        <script type="text/x-magento-init">
        {
            "*": { "Vendor_Module/js/modtoken": {
                    "token" : "<?php echo $block->escapeHtml(__($block->getToken()));  ?>"
                 } }
            }
        </script>
    </div>
</div>

requirejs-config.js

var config = {
    map: {
        '*': {
            testjs:'https://test.test1.com/js/v1/test.js'
        }
    }
};

This is js file app/code/Vendor/Module/view/frontend/web/js/modtoken.js

define([
    "jquery",
    "jquery/ui",
    "testjs"
], function ($) {
    "use strict";
    return function (config) {        
        Test.test1({
                token: config.token
            });
        console.log(config.token);        
    }    
});

But the above method is not working. Please help and suggest a method to call this file using require js.

12
  • When you see requirejs-config.js do you see your code mapped for hipsjs?
    – Gagan
    Commented Aug 2, 2018 at 7:34
  • Sorry, I didn't get you. Commented Aug 2, 2018 at 8:02
  • When you view source your website there is a link to javascript like _requirejs/frontend/Magento/luma/en_US/requirejs-config.js. If you open that file do you see the code that you wrote in requirejs-config.js
    – Gagan
    Commented Aug 2, 2018 at 8:04
  • Sorry no link with requirejs-config.js. But another file with name /frontend/Magento/luma/en_US/requirejs/require.js exist Commented Aug 2, 2018 at 8:06
  • it should be there, please share view source screensht of all js files list
    – Gagan
    Commented Aug 2, 2018 at 9:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.