Magento – Magento 2 How to add external Js in a page

consoleerrorjavascriptlayoutmagento2

I want to add an external js in my home page. The external js loads a popup form in my home page. I have tried the code in phtml file and also in xml file. In both case the form will appear. But some js errors in the console

https://prnt.sc/mo0mnj

Uncaught TypeError: Cannot read property 'version' of undefined

Uncaught TypeError: owl.owlCarousel is not a function

Here is my code

cms_index_index.xml

<body>       
   <referenceContainer name="before.body.end">
        <block class="Magento\Framework\View\Element\Template" name="constant_contact" template="Magento_Theme::constantcontact.phtml"/>
   </referenceContainer>
</body>

constantcontact.phtml

<script> var _ctct_m = "5454agf54b30f4dfb97a"; </script>
<script id="signupScript" src="//static.test.com/js/new-form-widget/current/test-form-widget.min.js" async defer></script>

Please help me

Best Answer

I was having a similar problem and this worked for me:

<page>
<head>

<link src="//static.test.com/js/new-form-widget/current/test-form-widget.min.js" src_type="url" />

</head>
</page>