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.