How can I make make the class of an entry link "active" if entry is the current url? I'm currently using the below jQuery but it's buggy as it relies on looking for 'similar' URL and not the exact URL.
Is there a way it can be done just using Twig?
{% set mainNavigation = craft.entries.section('mainNavigation').all() %}

&lt;div class=&quot;navbox-collapse collapse&quot; id=&quot;navbar-menu&quot;&gt;
    &lt;ul class=&quot;nav&quot; data-in=&quot;fade-In-Down&quot; data-out=&quot;fade-Out-Up&quot;&gt;

    {% nav page in mainNavigation %}

        &lt;li  class=&quot;{% if page.hasDescendants() %}topLevel dropdown {% else %}singleLevel{% endif %}&quot;&gt;
        
            {% if page.customUrl|length %}
                &lt;a {% if page.hasDescendants() %} class=&quot;dropdown-toggle&quot; data-toggle=&quot;dropdown&quot; {% endif %} href=&quot;{{ page.customUrl }}&quot;&gt;&lt;span&gt;{{ page.title }}&lt;/span&gt;&lt;/a&gt;
            {% else %}
                &lt;a {% if page.hasDescendants() %} class=&quot;dropdown-toggle pageLink&quot; data-toggle=&quot;dropdown&quot; {% endif %} href=&quot;{{ page.selectEntry.one().url }}&quot;&gt;&lt;span&gt;{{ page.title }}&lt;/span&gt;&lt;/a&gt; 
            {% endif %}
            
            {% ifchildren %}
                &lt;ul class=&quot;dropdown-menu megamenu-content {% if page.doubleColMenu.contains('twoxcol') %}twoxcol {% endif %}&quot;&gt;
                    {% children %}
                &lt;/ul&gt;
            {% endifchildren %}
        &lt;/li&gt;
            
    {% endnav %}
&lt;/div&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
    $(function(){
    var current = location.pathname;
    $('#navbar-menu li a').each(function(){
    var $this = $(this);
    // if the current path is like this link, make it active
    if($this.attr('href').indexOf(current) !== -1){
        $this.addClass('active');
    }
})
    })
&lt;/script&gt;
You don't need to use Javascript/jQuery for this.
You can get the the current url from craft.app.request in some way and do a check as you output your navigation.
It looks like you have some custom fields for urls but, For instance, if you wanted to check the last url segment you could use craft.app.request.segment(-1) to get it. Then when outputting your css classes, check it against whatever and output the active class.
{% set currentPage = craft.app.request.segment(-1) %}

then in your loop...

&lt;li class=&quot;{{ thisUrl == currentPage ? 'active ' }} otherclasses&quot;&gt;
    &lt;a href=&quot;{{ foo }}&gt;Some title&lt;/a&gt;
&lt;/li&gt;

You can also get the full URL with {{ craft.app.request.absoluteUrl }}
Or {{ craft.app.request.pathInfo }} to get the path