Magento 2 script tag and available options in layout xml

We can add a script via layout XML file in Magento 2.

The below format will stop the HTML parse until js downloaded and executed.

<script src="Bilal_Usean/js/sample.min.js" />

The below format will stop the HTML parse only on while executing js.

<script src="Bilal_Usean/js/sample.min.js" async="async" />

The below format will not stop the HTML parse, it will execute js after HTML parser completed.

<script src="Bilal_Usean/js/sample.min.js" defer="true"/>

Defer Vs Sync

  1. If async is present: The script is executed asynchronously with the rest of the page (the script will be executed while the page continues the parsing)

  2. If async is not present and defer is present: The script is executed when the page has finished parsing

  3. If neither async or defer is present: The script is fetched and executed immediately, before the browser continues parsing the page

Below picture clearly displays the difference between the async and defer ( I took it from here thanks for this author)

We can call the URL type of script as below.

<script src="" src_type="url" />

We can set charset, ie_condition.

<script src="Bilal_Usean/js/sample.min.js" charset="UTF-8" />
<script src="Bilal_Usean/js/sample.min.js" ie_condition="IE 9" />

Default script type is type="text/javascript" and we can change what we need

<script src="Bilal_Usean/js/sample.min.js" type="text/javascript" />

I hope it helps. If you need any clarification on this post, please mention in the comment section.

66 views0 comments

Recent Posts

See All

Magento 2 CMS block with the same ID already exists

I have seen the below error when I open cms block grid something went wrong with processing the default view and we have restored the filter to its original state. I found the below log in the excepti

How to generate bearer token - Magento 2 REST API

We can generate the admin bearer token using the below command. Please run the below command in the terminal to generate a token. curl -XPOST -H 'Content-Type: application/json'

How to update multiple products using Magento 2 REST API

We can able to update multiple products in the single Magento REST API request using bulk endpoints. Also, before using the Bulk API to process messages, you must install and configure RabbitMQ, which