Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-19T12:44:02.543Z
Answers.LastActivityDate: 2016-01-19T12:44:02.543Z
Answers.UserDisplayName: William Isted
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-19T12:17:35.987Z
Answers.LastActivityDate: 2016-01-19T12:46:45.313Z
Answers.LastEditDate: 2016-01-19T12:46:45.313Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Tags: <files><includejs>
Title: how to includeJsFile in head instead of body
ThreadText: # 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 `<body>`:
  
  ```
  {% includeJsFile '/path/script.min.js' %}
  
  ```
  
  I need to add it to the `<head>` 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`](https://craftcms.com/docs/templating/functions#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 `<body>`. 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 `<head>`.
  
  Another thing you could do is to not use the `includeJsFile` function to add the HTML to the `<head>`, 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 = '<script src="{{ url('assets/javascripts/myscript.js') }}"></script>' %}
  
  {% block head %}
      {{ parent() }}
  
      <!-- Scripts -->
      <script src="{{ url('assets/javascripts/myscript.js') }}"></script>
  {% endblock %}
  
  ```
  
  base.html
  
  ```
  <head>
      {% block head %}
          <!-- Document head -->
          <meta charset="utf-8">
  
          {% if headHtml is defined %}
              <!-- Scripts -->
              {{ headHtml }}
          {% endif %}
      {% endblock %}
  </head>
  
  ```
  
  
  ---
  
  ### 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.*
  
  
  ---
  
  