Magento 2 – How to Add External JS

javascriptmagento2

I want to add the following link in the head tag, but its returning 404 error. Can anyone help me on this?

<script  type="text/javascript"  src="https://www.google.com/recaptcha/api.js"></script>

Best Answer

I'd recommend using the script method rather than the text method, it's easier for other developers to understand, it's less code, and meets Magento's official instructions.

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>

Results

enter image description here

Related Topic