4

I need to add a JS script on the head tag, but only in the Success page. Inside Magento 2, I have no idea how doing this.

Someone can help me? :)

2 Answers 2

1
  1. create the checkout_onepage_success.xml file at following location on your custom extension

app\code\Vendor\Extension\view\frontend\layout\checkout_onepage_success.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <script src="Vendor_Extension::js/yourcustom.js"/>
    </head>
</page>
  1. now create yourcustom.js on the following location

app\code\Vendor\Extension\view\frontend\web\js\yourcustom.js

  1. now in yourcustom.js added your code what ever you want to add.
2

You need to create checkout_onepage_success.xml layout in your module then adds the below code inside the page tag

<head>
    <script src="Vendor_Module::js/custom.js"/>
</head>

After that, you can create custom.js file inside your custom module.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.