LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Highlight/Active Current Page Menu Link Automatically Via JavaScript

Highlight/Active Current Page Menu Link Automatically Via JavaScript

Highlight-Active-Current-Page-Menu-Link-Automatically-Via-JavaScript
How we can highlight the current page, menu, or link? We can do this with HTML CSS & JavaScript. Check Out This Highlight Current Page With HTML CSS JS, Show Active Link Or Menu.

I am sure that, You have seen underlining on the current menu or page on mostly all websites. When we click on any website about then an underline appears on the about menu‘s bottom section. Some websites have a special effect to indicate the current opened menu or link.

Learn how to highlight the currently active page in your website’s navigation bar/menu. A common website feature is to highlight the current page menu item when the user navigates from page to page.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Features:

  1. Light Weight.
  2. Pure Vanilla JavaScript Code.
  3. No External Files.
  4. Work With Any Format Of Date.
  5. Fully Customizable.
Recommended For You:
Open Different DIV Panel In A Page With Sliding Effect

How To Highlight/Active Current Page Menu Link Automatically Via JavaScript?

There are a few easy and understandable steps to achieve your desired functionality using pure PHP that we are gonna share below. Follow each step perfectly.

JavaScript:

<script type="text/javascript">
const confirmEnding = (str, target) => new RegExp(`${target}$`, '').test(str);
//confirmEnding("Bastian", "n")
function setOpenedPageNav() {
var pathName = document.location.pathname;
var menu_list = document.querySelectorAll('.sidebar_menu a'); 
// returns NodeList
for (var item of menu_list) {
if(confirmEnding(item.href,pathName) == true){
console.log(pathName);
item.classList.add("active");
} 
}
}
setOpenedPageNav();
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave your precious feedback in our comment form below. Happy development, See you in the next article.

Recommended For You:
Simple And Clean Double Value CSS Badges And Labels Examples

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *