<p>As of Craft 3 (Tested using <a href="https://github.com/craftcms/cms/releases/tag/3.0.0-RC5" rel="noreferrer">RC5</a>) you can do it like this: </p>

<pre><code>{% do view.registerJsFile("path/to/script.js", {"async":"async", "defer":"defer"}) %}
</code></pre>

<p>Source: <a href="http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerJsFile()-detail" rel="noreferrer">Documentation for Yii's view.registerJsFile()</a></p>

CC BY-SA 3.0
2018-01-15T16:47:12.310Z
2018-01-15T16:47:12.310Z
stojda
<p>Knowing that <code>&lt;script&gt;</code>s are imploded with <a href="https://github.com/craftcms/cms/blob/2.6.2986/src/services/TemplatesService.php#L760" rel="nofollow noreferrer">\n</a> char. You can add async defer this way:</p>

<pre><code>{%- set scripts = getFootHtml() | split("\n")  -%}
{%- for script in scripts -%}
    {%- if script matches "%\/assets\/js\/main\.js%" or ' src=' not in script -%}
        {{- script | raw -}}
    {%- else -%}
        {{- script | replace('script type', 'script async defer type') | raw -}}
    {%- endif -%}
{%- endfor -%}
</code></pre>

CC BY-SA 3.0
2017-07-07T14:42:05.887Z
2017-07-07T14:42:05.887Z
Krzysztof Boduch
<p>Not as of <a href="https://craftcms.com/changelog#build2791" rel="nofollow">Craft 2.6.2791</a>.</p>

<p>If that's a requirement for you, you can not use that tag and put a block in your footer and manually create the script tag with <code>async defer</code> set.</p>

CC BY-SA 3.0
2016-06-19T12:40:24.617Z
thanks for the feedback Brad - i'll look in to an alternative way of implementing it.
alexr
CC BY-SA 3.0
2017-10-23T16:21:55.757Z
@alexr how did you implement this in the end? I am currently at the same conundrum.
Terry Upton
CC BY-SA 3.0
2017-10-26T15:17:31.107Z
@TerryUpton i think in the end i ended up manually putting it in the footer of my site in an if statement. In this particular site I was setting a title var in each template in twig anyway, and only had a map on one page, so i ended up doing something like {% if title == 'mappage' %}<script src="path/to/script" async defer></script>{% endif %}
alexr
CC BY-SA 3.0
2016-06-16T18:44:20.210Z
2016-06-16T18:44:20.210Z
Brad Bell
<p>Is there a way to set <code>async defer</code> on a <code>&lt;script&gt;</code> tag with the built in: </p>

<pre><code>{% includeJsFile %}
</code></pre>

<p>It's for Google Maps e.g.: </p>

<pre><code>&lt;script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&amp;callback=initMap"
async defer&gt;&lt;/script&gt;
</code></pre>

<p>I'd still like to be able to output this at the bottom of the layout from within another template. Is this possible?</p>

<includejs>
Can you set async defer when using {% includeJsFile %}?
# Can you set async defer when using {% includeJsFile %}?

(Asked by: username_0 on 2016-06-16)

Is there a way to set `async defer` on a 

I'd still like to be able to output this at the bottom of the layout from within another template. Is this possible?

**Tags:** includejs

---

## 3 answers

### Answer by username_3 on 2016-06-16. Score: 4 | Accepted answer

Not as of [Craft 2.6.2791](https://craftcms.com/changelog#build2791).

If that's a requirement for you, you can not use that tag and put a block in your footer and manually create the script tag with `async defer` set.

> **Comments:**
> **username_0**: thanks for the feedback Brad - i'll look in to an alternative way of implementing it.  
> **username_4**: @alexr how did you implement this in the end? I am currently at the same conundrum.  
> **username_0**: @TerryUpton i think in the end i ended up manually putting it in the footer of my site in an if statement. In this particular site I was setting a title var in each template in twig anyway, and only had a map on one page, so i ended up doing something like {% if title == 'mappage' %}<script src="path/to/script" async defer></script>{% endif %}  

---

### Answer by username_1 on 2018-01-15. Score: 8

As of Craft 3 (Tested using RC5) you can do it like this: 
{% do view.registerJsFile("path/to/script.js", {"async":"async", "defer":"defer"}) %}

Source: Documentation for Yii's view.registerJsFile()


---

### Answer by username_2 on 2017-07-07. Score: 1

Knowing that


---

