0

I have a CMS page in Magento 2, for which I want to add <script> tag that will be included in the <head> section of this page only.

I do not want this <script> tag to appear on any other page. My Magento version is 2.4.2

Thank you in advance !!

1 Answer 1

0

You have to create a layout file specifically for your CMS Page, such as in layout folder of your custom module,

cms_page_view_selectable_<CMS Page Identifier>_<Layout Update Name>.xml

for example if my CMS page identifier name is testing, I'll name it as cms_page_view_selectable_testing_loginform.xml and inside the file I can write code as below to get the login form and remember me option as well:

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <link src="[Vendor]_[Module]::js/js_fileName.js"/>
    </head>
</page>

And than go to your CMS page in admin and from there under design tab, select your option in Custom Layout Update, whatever you've named it, here I have named it loginform, so I'll select loginform inside my Custom Layout Update.

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.