<p>You can queue files with <code>includeJsFile</code>, <code>includeJs</code> and output <code>getFootHtml()</code> anywhere on your page.</p>

<p>Once you've called <code>getFootHtml()</code> the queue will be cleared allowing you to use <code>includeJsFile</code> again later in your template i.e. both in the <code>head</code> and <code>body</code>.</p>

<p><em>This doesn't appear in the documentation.</em></p>

CC BY-SA 3.0
2016-01-19T12:44:02.543Z
2016-01-19T12:44:02.543Z
William Isted
<p>You can use the <a href="https://craftcms.com/docs/templating/functions#getFootHtml" rel="noreferrer"><code>getFootHtml</code></a> function to tell Craft where to output HTML queued up with <code>includeJsFile</code> and <code>includeJs</code>. You'd normally have that tag somewhere at the end of your <code>&lt;body&gt;</code>. But all it does is to get all queued up HTML and output it, so if your JS are already "included", you can use it in <code>&lt;head&gt;</code>.</p>

<p>Another thing you could do is to not use the <code>includeJsFile</code> function to add the HTML to the <code>&lt;head&gt;</code>, but use a different approach. Here's an example using a var and an example using a dedicated Twig block:</p>

<p>index.html</p>

<pre><code>{% extends 'base.html' %}

{% set headHtml = '&lt;script src="{{ url('assets/javascripts/myscript.js') }}"&gt;&lt;/script&gt;' %}

{% block head %}
    {{ parent() }}

    &lt;!-- Scripts --&gt;
    &lt;script src="{{ url('assets/javascripts/myscript.js') }}"&gt;&lt;/script&gt;
{% endblock %}
</code></pre>

<p>base.html</p>

<pre><code>&lt;head&gt;
    {% block head %}
        &lt;!-- Document head --&gt;
        &lt;meta charset="utf-8"&gt;

        {% if headHtml is defined %}
            &lt;!-- Scripts --&gt;
            {{ headHtml }}
        {% endif %}
    {% endblock %}
&lt;/head&gt;
</code></pre>

CC BY-SA 3.0
2016-01-19T12:17:35.987Z
2016-01-19T12:46:45.313Z
2016-01-19T12:46:45.313Z
carlcs
<p>I am using this code to include a js file, but it adds to the <code>&lt;body&gt;</code>:</p>

<pre><code>{% includeJsFile '/path/script.min.js' %}
</code></pre>

<p>I need to add it to the <code>&lt;head&gt;</code> instead, how can I do?</p>

<files><includejs>
how to includeJsFile in head instead of body
# how to includeJsFile in head instead of body

(Asked by: username_0 on 2016-01-19)

I am using this code to include a js file, but it adds to the :
{% includeJsFile '/path/script.min.js' %}

I need to add it to the  instead, how can I do?

**Tags:** files, includejs


---

## 2 answers

### Answer by username_2 on 2016-01-19. Score: 4 | Accepted answer

You can use the getFootHtml function to tell Craft where to output HTML queued up with includeJsFile and includeJs. You'd normally have that tag somewhere at the end of your . But all it does is to get all queued up HTML and output it, so if your JS are already "included", you can use it in .
Another thing you could do is to not use the includeJsFile function to add the HTML to the , but use a different approach. Here's an example using a var and an example using a dedicated Twig block:
index.html
{% extends 'base.html' %}

{% set headHtml = '' %}

{% block head %}
    {{ parent() }}

    
    
{% endblock %}

base.html

    {% block head %}
        
        

        {% if headHtml is defined %}
            
            {{ headHtml }}
        {% endif %}
    {% endblock %}


---

### Answer by username_1 on 2016-01-19. Score: 2

You can queue files with `includeJsFile`, `includeJs` and output `getFootHtml()` anywhere on your page.

Once you've called `getFootHtml()` the queue will be cleared allowing you to use `includeJsFile` again later in your template i.e. both in the `head` and `body`.

*This doesn't appear in the documentation.*


---

