Show active color if no filename is given

Hello, i have this script to show an active color on nav links, this works fine, but it only works for a home page address like this: http://mydomain/myfolder/index.php how can i get it to work for: http://mydomain/myfolder/

`jQuery(document).ready(function($){
var url = window.location.pathname;
$('.nav a').filter(function() {
return this.pathname == url;
 })
.closest('li')
.addClass('current_yes');

$('.nav-submenu a').filter(function() {
return this.pathname == url;
})
.closest('li')
.addClass('sub_current_yes')
.parents('li')
.addClass('current_yes');
});`

Thank you.

1 Like

If I understood, you are not able to see your home page working without specifiying the exact page path (i.e. including “index.php”)

In this case you only need to configure your web server (e.g. apache or whatever) to accept “index.php” as a default index file. Usually, in apache, there’s a config file (such as .htaccess) where everything is written in.

1 Like

No this is not what i menth. This is about getting the active color on the nav links.
So if i enter the site with http://mydomain/myfolder/index.php then the Home link in the navigation will turn to black and white text, but if i enter the site with http://mydomain/myfolder/ then the Home link will be red and white text (this is the colors for the non active nav links.

Hope you understand what i mean?

Thank you.

1 Like

Maybe if you could show the html structure of your page could be fine.

Anyway, the problem seems to concern the value of window.location.pathname, which should be exactly “/myfolder/index.php” in the first case and only “/myfolder/” in the latter. Probably your “this.pathname” value includes the “index.php” string as well, so you should manage this case, for example by setting the filter function as something like the following:

$('.nav a').filter(function() { return this.pathname === url || this.pathname.replace("index.php", "") === url; })

Hope this helps.

1 Like

Hey, this semes to work. Thank you so much.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.