I want to add a class to a menuepoint if one path is selected.
It would be easy if every site was its own .php
/.html
file, however everything is ruled in one .php
file and everything is navigated over actions (?action=main
, ?action=userinformation
).
I am searching for an alternative to get this work with the path and the action.
if (location.pathname == "/index.php") {
$("#main1").addClass("mainActive");
} else if (location.pathname == "/index.php?action=other") {
$("#main2").addClass("mainActive");
}
location.pathname
doesn't include the query string. But you can combine it with location.search
:
var path = location.pathname + location.search;
// "/index.php" + "?action=other"
if(path === "/index.php?action=other") {
// ...
}
Maybe you can use like;
var s = location.search;
if (s === "" || s === "?" || s.indexOf("?action=main") > -1)
// main active
else if (s.indexOf("?action=userinformation") > -1)
// userinformation active
// ... and so on