• Home

    How to write this simple javascript or jquery?

    I have some navigation links, for example:

    <ul id="nav">
       <li><a href="index.html">Home</a>
       <li><a href="about.html">About</a>
       <li><a href="contact.html">Contact</a>
    </ul>
    

    How do I add a CSS class with a name activeto the opening tag <li>of a list item that contains a a hrefvalue that matches the current URL?

    For example, if the current page the user is on is located about.html, then the navigator should look like this:

    <ul id="nav">
       <li><a href="index.html">Home</a>
       <li class="active"><a href="about.html">About</a>
       <li><a href="contact.html">Contact</a>
    </ul>
    

    Note:

    URLs can have additional parameters, for example:

    about.html = Foo bar &? Bar = Loo

    therefore, everything that is used to detect url should not take parameters into account, but simply the name of the page and extension.

    I would prefer to achieve this in plain JavaScipt, since I am not using jQuery for anything else on the site, but that's fine.

    Edit

    index.html URL-, , , :

    http://www.sitename.com/
    

    , , home list.

    +3
    3

    JQuery

    if(window.location.pathname === '') {
         $('#nav li:first-child').addClass('active');
    }
    else {
        var path = window.location.pathname;
        path = path.substr(path.lastIndexOf('/') + 1);
        $('#nav li').filter(function(index) {            
            return path === $(this).children('a').attr('href');
        }).addClass('active');
    }
    

    JavaScript:

    var menu_elements = document.getElementById('nav').children;
    if(window.location.pathname === '') {
         menu_elements[0].className += ' active';
    }
    else {
        var path = window.location.pathname;
        path = path.substr(path.lastIndexOf('/') + 1);
        for(var i = menu_elements.length; i--;) {
            var element = menu_elements[i];
            var a = element.children[0];
            if(a.href === path) {
                element.className += ' active';
                break;
            }
        }
    }
    

    : children[] FF 3.0. children, getElementsByTagName.

    +4

    window.onload=function() {
      var activeLi;
      if (location.pathname) { 
        var fileName = location.pathname.substring(pathname.lastIndexof('/')+1);
      /* just take the start - 
         not handling filenames that are substrings of other filenames 
         nor filenames with more than one dot. */
        fileName = fileName.split('.')[0]; 
        var links = document.getElementById('nav').getElementsByTagName('a');
        for (var i=0;i<links.length;i++) {
          if (links[i].href.indexOf(fileName)==0) { // starts with filename
            activeLi = links[i].parentNode;
            break; 
          }
        }
      }
      else { // no page given
        activeLi = document.getElementById('nav').getElementsByTagName('li')[0];
      }
      if (activeLi) activeLi.className="active";
    }
    

    ADD Name, LI, , jQuery, .

    +1
    
    //Get sub-domain url 
    var currentUrl = window.location.href,
        splitUrlArr = currentUrl.replace(/\?.*/,'').split('\/');
        subDomainUrl = splitUrlArr[splitUrlArr.length-1];
    //if url matches the site home url, add classname 'active' to first li
    if(splitUrlArr.join('\/') == currentUrl) {
        document.getElementById('nav').getElementsByTagName('li')[0].className = "active";
    }else {
        //Find the matching href and add className 'active' to its parent li
        var targetLi = null;
        var links = document.getElementById('nav').getElementsByTagName('a');
        for (var i=0; i < links.length; i++) {
            if (links[i].href === subDomainUrl) { 
                targetLi = links[i].parentNode;
                break; 
            }
        }
        if(targetLi) { targetLi.className = "active"; }
    }
    
    0
    source

    Source: https://habr.com/ru/post/1789768/


    All Articles