Advertisement
Guest User

link-active.js

a guest
Sep 30th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function stripTrailingSlash(str) {
  2.     if (str.substr(-1) == '/') {
  3.         return str.substr(0, str.length - 1);
  4.     }
  5.     return str;
  6. }
  7.  
  8. var url = window.location.pathname;
  9. var activePage = stripTrailingSlash(url);
  10.  
  11. var array = activePage.split('/');
  12. var page = '/' + array[1];
  13.  
  14. $('.nav li a').each(function () {
  15.     var currentPage = stripTrailingSlash($(this).attr('href'));
  16.  
  17.     if (page == currentPage) {
  18.         $(this).parent().addClass('active');
  19.     }
  20. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement