I have Magento 2.3. How can I inject external JS file to my module with together with params. For example I have
require-config.js let config = { paths: { 'my-external-file': [ 'https://externalfile.com/external' ] } }; phtml file require(['jquery', 'my-external-file', 'domReady!'], // Is it possible to pass a variable from here and get the following result: https://externalfile.com/external.js?param1=value¶m2=value1 });
Hello @Ovidijus10eca0
To do this use the same script or link XML as normal but include src_type="url". As noted in the official docs
<?xml version="1.0" ?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <head> <script src="https://www.google.com/recaptcha/api.js" src_type="url"/> </head> </page>
Kindly refer below link:
https://devdocs.magento.com/guides/v2.3/frontend-dev-guide/layouts/xml-manage.html#layout_markup_css
It may help you!
Thank you
But wait, I need to have params dynamic (get params values from PHP)
if I use this approach(XML file), can I have dynamic params?
<head> <script src="https://www.google.com/recaptcha/api.js" src_type="url"/> </head>
also noticed what using XML, I can pass only one param to my js file
../example.js?param=value (work) ../example.js?param=value¶m2=value (don't work, with syntax error, because of "&" I think)
With require js you can't pass variable.
You need to add js file with this which should be save in your module. Then you need to use that js file with Ajax which help you to pass variable value to third party.
Thanks