I had to give my links an active state and used a code that gets the second part of the url
and searches for the nav li a
element with an href
equal to the second part of the url
.
When I go to my site the first like U will be is www.domain.com
without any /index.php
or /contact-us.php
. This is a problem since the second part of my url will be empty and tell all my a
elements to be active.
When I press on 1 of the header links everything works fine. This is the site I'm talking about, see it yourself if you want ☺.
Code:
$(function () {
var current = location.pathname;
//Here
$('nav 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('selected');
}
})
});
I found this code from this stackoverflow question.
I tried adding the underneath code on the //Here
spot of above code:
if ($this.attr('href') === window.location.href){
$this.attr('href', window.location.href + "/index.php"');
}
But as you can see I'm not an expert in coding (yet ☺ ) so I couldn't get this to work. Could someone help me with this or guide me in the right direction?