<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>

CC BY-SA 3.0
2017-02-21T00:31:34.763Z
2017-02-21T00:31:34.763Z
Phil H
<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>

CC BY-SA 3.0
2017-02-21T00:38:21.737Z
2017-02-21T00:38:21.737Z
2017-04-13T12:51:00.200Z
andrew.welch
<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>

CC BY-SA 3.0
2017-02-20T23:53:22.817Z
2017-02-21T00:08:56.387Z
2017-02-21T00:08:56.387Z
Stephen Lewis
<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>

<templating>
Display JavaScript only on the homepage
# 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?
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
LoadJS as a Lightweight JavaScript Loader


---

### Answer by username_3 on 2017-02-20. Score: 2

You'd typically use the {% includeJs %} or {% includeJsFile %} tags within your page template, to specify your page-specific JavaScript, and then output everything in your layout template by calling {{ 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 #}





{# This outputs the JavaScript from mypage.twig #}
{{ getFootHtml() }}


---

