XML : Magento 2 - Running a JS script from the layout body

Upon a page load, I want to run three different scripts in a specific order. One of the scripts fetches the data from the server code using block class, while the other two don't. The problem is that I can run the scripts that don't require any data from the server without any problems inside the head tag. But the script that requires data from the server, in my limited knowledge can only be run inside a referenceContainer tag inside a body tag.

This is leading to a really ugly piece of code. I'm having to specify the first script inside the head tag, the second one (the one that fetches data) inside a body tag and the third one inside another head tag.

default.xml

  <?xml version="1.0"?>  <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">      <head>          <script src="Company_Project::keen.js"/>      </head>        <body>          <referenceContainer name="main">              <block class="Company\Project\Block\Adminhtml\System\Config\Export" template="dropCookie.phtml"/>          </referenceContainer>      </body>        <head>          <script src="Company_Project::dropCookie.js"/>      </head>  </page>    

Anyway that I can run that block tag inside the head tag? Instead of starting the body tag?

Apologies that my PHP/Magento knowledge is very limited. I basically followed this tutorial to learn how to use the block class to get data from the server side. If there is another, more elegant want to achieve the same please do suggest. Thanks

No comments:

Post a Comment