Adding async attribute to scripts tags generated by {{ script_tag }}

Highlighted
Tourist
10 0 6

Hi guys,

Trying to get certain scripts on my site to load asynchronously to hopefully improve page load times.  A couple of scripts in the theme are being generated like this:

{{ 'example.js' | asset_url | script_tag }}

This means I can't just add the "async" within the tags as I would normally.  Is there a way to have the tags generated so they will load asynchronously?  The image_tag filter allows you to add additional parameters for alt text, I wondered if there was anything like this for script_tag maybe?

Thanks!

1 Like
Shopify Staff
Shopify Staff
5704 0 278

Hi Fiona!

Change this:

{{ 'example.js' | asset_url | script_tag }}

... to that:

<script src="{{ 'example.js' | asset_url }}" async></script>

 

Caroline from http://11heavens.com ∴ mllegeorgesand AT gmail DOT com
2 Likes
Tourist
10 0 6

Thanks Caroline, that's perfect!

0 Likes
New Member
3 0 0

Hi Caroline,

 

All perfect. But, the arrtibute "async is also printed on my screen. How to resolve this?

 

Many thanks in advance.

 

Regards,

Manoon

0 Likes