Highlight current page button based on another page

0

I have the menu down, which is contained in the page empresas.php and to "Edit" a company becomes editar-empresa.php?id=x :

Thefunctionthataddstheclassactivetothecurrentpageisasfollows:

(function(){varnav=document.getElementById('menu'),anchor=nav.getElementsByTagName('a'),current=window.location.href.split("?")[0];
        for (var i = 0; i < anchor.length; i++) {
            if(anchor[i].href.split("?")[0] == current) {
                anchor[i].children[0].className = "active";
            }
    }
})();

That is, the "Business" section will remain highlighted only if it is on the empresas.php page, but I need it to remain highlighted when going to the editar-empresa.php?id=x subsection. What should I do to achieve this?

HTML:

<div id="menu">
    <nav>
        <ul>
            <a href="emissor-nfe.php"><li>Emissor NF-e</li></a>
            <a href="empresas.php"><li>Empresas</li></a>
            <a href="clientes.php"><li>Clientes</li></a>
            <a href="produtos.php"><li>Produtos</li></a>
            <a href="transportadoras.php"><li>Transportadoras</li></a>
            <a href="logout.php"><li>Sair</li></a>
        </ul>
    </nav>
</div>
    
asked by anonymous 01.07.2017 / 03:19

1 answer

0

Following the advice of @AndersonCarlosWoss, a possible solution is to rename the page name editar-empresas.php to empresas-editar.php and use the indexOf() function:

(function () {
    var nav = document.getElementById('menu'),
        anchor = nav.getElementsByTagName('a'),
        current = window.location.href.split("?")[0];
    for (var i = 0; i < anchor.length; i++) {
        if (current.indexOf(anchor[i].toString().split(".")[0]) >= 0) {
            anchor[i].children[0].className = "active";
        }
    }
})();

Note: the split() function was used to disregard the .php extension (remembering that it only works with strings and so the toString() method was used). >     

19.07.2017 / 01:30