Answers.Body: <p>If you want something to only appear on the homepage you could use this:</p>
  
  <pre><code>{% if craft.request.lastSegment == "" %}
    {# do something #}
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-21T00:31:34.763Z
Answers.LastActivityDate: 2017-02-21T00:31:34.763Z
Answers.UserDisplayName: Phil H
Answers.Body: <p>So the most reliable way to check to see if you're on the Craft-designated homepage (which can be either a Single, or a Structure, or what have you) would be:</p>
  
  <pre><code>{% if entry.slug == '__home__' %}
      //Content goes here
  {% endif %}
  </code></pre>
  
  <p>As per: <a href="https://craftcms.stackexchange.com/questions/1467/add-home-page-to-a-structure">Add home page to a structure?</a></p>
  
  <p>Rather than using <code>{% includeJs %}</code> or <code>{% includeJsFile %}</code> I would recommend that you use some kind of a JavaScript loader with dependency management:</p>
  
  <p><a href="https://nystudio107.com/blog/using-systemjs-as-javascript-loader" rel="nofollow noreferrer">Using SystemJS as a JavaScript Loader</a></p>
  
  <p><a href="https://nystudio107.com/blog/loadjs-as-a-lightweight-javascript-loader" rel="nofollow noreferrer">LoadJS as a Lightweight JavaScript Loader</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-21T00:38:21.737Z
Answers.LastActivityDate: 2017-02-21T00:38:21.737Z
Answers.LastEditDate: 2017-04-13T12:51:00.200Z
Answers.UserDisplayName: andrew.welch
Answers.Body: <p>You'd typically use the <a href="https://craftcms.com/docs/templating/includejs" rel="nofollow noreferrer"><code>{% includeJs %}</code></a> or <a href="https://craftcms.com/docs/templating/includejsfile" rel="nofollow noreferrer"><code>{% includeJsFile %}</code></a> tags within your page template, to specify your page-specific JavaScript, and then output everything in your layout template by calling <a href="https://craftcms.com/docs/templating/functions#getFootHtml" rel="nofollow noreferrer"><code>{{ getFootHtml() }}</code></a>.</p>
  
  <p>Here's how your (very basic) page template might look in practice:</p>
  
  <pre><code>{# mypage.twig #}
  {% extends '_layout' %}
  {% includeJsFile "/assets/js/mypage.js" %}
  </code></pre>
  
  <p>And here's how your (equally basic) layout template might look:</p>
  
  <pre><code>{# _layout.twig #}
  &lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;&lt;/head&gt;
  &lt;body&gt;
  
  {# This outputs the JavaScript from mypage.twig #}
  {{ getFootHtml() }}
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-20T23:53:22.817Z
Answers.LastActivityDate: 2017-02-21T00:08:56.387Z
Answers.LastEditDate: 2017-02-21T00:08:56.387Z
Answers.UserDisplayName: Stephen Lewis
Body: <p>What is the best method to render Javascript on specific page using name instead of id. This is what I found but not sure how to use name instead. </p>
  
  <pre><code>{% if entry.id == 2 %}
  {% endif %}
  
  {% if entry.section == homepage %}
      //Content goes here
  {% endif %}
  </code></pre>
  
Tags: <templating>
Title: Display JavaScript only on the homepage
ThreadText: # Display JavaScript only on the homepage
  
  (Asked by: username_0 on 2017-02-20)
  
  What is the best method to render Javascript on specific page using name instead of id. This is what I found but not sure how to use name instead.
  
  ```
  {% if entry.id == 2 %}
  {% endif %}
  
  {% if entry.section == homepage %}
      //Content goes here
  {% endif %}
  
  ```
  
  **Tags:** templating
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2017-02-21. Score: 2
  
  If you want something to only appear on the homepage you could use this:
  
  ```
  {% if craft.request.lastSegment == "" %}
    {# do something #}
  {% endif %}
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2017-02-21. Score: 2
  
  So the most reliable way to check to see if you're on the Craft-designated homepage (which can be either a Single, or a Structure, or what have you) would be:
  
  ```
  {% if entry.slug == '__home__' %}
      //Content goes here
  {% endif %}
  
  ```
  
  As per: [Add home page to a structure?](https://craftcms.stackexchange.com/questions/1467/add-home-page-to-a-structure)
  
  Rather than using `{% includeJs %}` or `{% includeJsFile %}` I would recommend that you use some kind of a JavaScript loader with dependency management:
  
  [Using SystemJS as a JavaScript Loader](https://nystudio107.com/blog/using-systemjs-as-javascript-loader)
  
  [LoadJS as a Lightweight JavaScript Loader](https://nystudio107.com/blog/loadjs-as-a-lightweight-javascript-loader)
  
  
  ---
  
  ### Answer by username_3 on 2017-02-20. Score: 2
  
  You'd typically use the [`{% includeJs %}`](https://craftcms.com/docs/templating/includejs) or [`{% includeJsFile %}`](https://craftcms.com/docs/templating/includejsfile) tags within your page template, to specify your page-specific JavaScript, and then output everything in your layout template by calling [`{{ getFootHtml() }}`](https://craftcms.com/docs/templating/functions#getFootHtml).
  
  Here's how your (very basic) page template might look in practice:
  
  ```
  {# mypage.twig #}
  {% extends '_layout' %}
  {% includeJsFile "/assets/js/mypage.js" %}
  
  ```
  
  And here's how your (equally basic) layout template might look:
  
  ```
  {# _layout.twig #}
  <!doctype html>
  <html>
  <head></head>
  <body>
  
  {# This outputs the JavaScript from mypage.twig #}
  {{ getFootHtml() }}
  </body>
  </html>
  
  ```
  
  
  ---
  
  