JavaScript Change link to active onclick change active link color css,change active link color,change

I have a common header for all of the pages I have in my site.

When I click on any link it takes me to that page and the page gets refreshed.

But the header is the same as it was in the previous page.

What I want is that when I click on any link, that link gets bold(Active) and if I am on homepage the home link should be active by default.

I am trying to work on this and when I click on any link the link gets active(bold) but as the page gets refresh and take me to the new page the link comes to non active(normal).

Here is my code.

<script type='text/javascript'>
$(window).load(function(){
$("#main-menu a").click(function() {
    $('a').removeClass('active');
    $(this).addClass("active");
});
});>  
</script>
<style>
.active {font-weight:bold}
</style>
<!--Navigation-->
    <nav id="main-menu">
        <ul class="right">
        <li><a target='_blank' href="#">Home</a></li>                                    
        <li><a target='_blank' href="#">Test Link 1</a></li>
        <li><a target='_blank' href="#">Test Link 2</a></li>
        <li><a target='_blank' href="#">Test Link 3</a></li>
    </ul>
      </nav>

Is there any way we can do it with the common header? Thanks,

Answer:1

You can check for example the window.location.pathname an compare it to the href of the navigation. Meaning:

var pathName = window.location.pathname;

$('nav a').each(function() {
    var $self = $(this);
    if($self.attr('href') ==pathName){
        $self.addClass('active');
    }
})

Something like that.

Answer:2

You should not remove the class active of a but of .active. Basically this clears all the classes .active so that you can set it to the currently clicked link.

Script would look like:

<script type='text/javascript'>
$(window).load(function(){
$("#main-menu a").click(function() {
    $('.active').removeClass('active');
    $(this).addClass("active");
});
});>  
</script>
<style>
.active {font-weight:bold}
</style>
<!--Navigation-->
    <nav id="main-menu">
        <ul class="right">
        <li><a target='_blank' href="#">Home</a></li>                                    
        <li><a target='_blank' href="#">Test Link 1</a></li>
        <li><a target='_blank' href="#">Test Link 2</a></li>
        <li><a target='_blank' href="#">Test Link 3</a></li>
    </ul>
</nav>

JSFiddle Demo

Answer:3

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script> var siteTitle = $.ajax({ url: 'http/', type: 'POST', ...

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script> var siteTitle = $.ajax({ url: 'http/', type: 'POST', ...

  1. make javascript load faster
  2. javascript load image faster

I have a page which has 2 text areas and wanted to validate each text area if the characters being typed is more than max length upon clicking submit button. Problem with the code below when clicking ...

I have a page which has 2 text areas and wanted to validate each text area if the characters being typed is more than max length upon clicking submit button. Problem with the code below when clicking ...

  1. jquery validation multiple textarea

first of all I apologize for my bad English, I want to make a JavaScript for a tabele who makes himself automatically by the ID Bold, just like this one here but the code is not only for weeks for ...

first of all I apologize for my bad English, I want to make a JavaScript for a tabele who makes himself automatically by the ID Bold, just like this one here but the code is not only for weeks for ...

On my webpage, I set my pdf as follow: <object data="myURL" type="application/pdf" width="300" height="200"></object> where the URL is created with javascipt from a blob like this: var ...

On my webpage, I set my pdf as follow: <object data="myURL" type="application/pdf" width="300" height="200"></object> where the URL is created with javascipt from a blob like this: var ...

  1. embedded save to flash
  2. save html with embedded images
  3. save pdf with embedded fonts
  4. save powerpoint with embedded fonts
  5. save powerpoint with embedded video
  6. save ppt with embedded fonts
  7. save powerpoint with embedded fonts mac
  8. save word with embedded objects as a pdf
  9. save powerpoint with embedded media
  10. save pdf with embedded documents
  11. save ppt with embedded video
  12. save pdf with embedded files
  13. save word with embedded fonts