www.webdeveloper.com
Results 1 to 10 of 10

Thread: current link menu marker

  1. #1
    Join Date
    Oct 2007
    Posts
    15

    current link menu marker

    When someone is on a page, I want the style of that current menu link to be obvious to the user. Change color, make bold, etc.

    I found this to get the current page...

    Code:
    <script type="text/javascript">
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    </script>
    ... but how do I use this to change the class of the link that matches the current page?

    TIA

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Oct 2007
    Posts
    15

    thanks, but...

    You r suggestion obviously will work, but I'm try to do this w/o having to make code changes to every page.

    Basically, it's:

    if var current_page == var link, style.link = different_css_style

    I'm trying to do this with JS (I'm not a JS expert by any means) so I do not have to edit each page, and each line in the menu.

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    This basically
    Code:
    window.onload=function() {
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    var aObj=document.getElementsByTagName('a');
    var i=aObj.length; 
    while(i--) { 
        if(aObj[i].href.indexOf(sPage )!=-1) {aObj[i].className = 'current';}
        }
    };
    At least 98% of internet users' DNA is identical to that of chimpanzees

  5. #5
    Join Date
    Oct 2007
    Posts
    15

    trying...

    Thanks.

    I added the script to the header, and created a .current class, but I'm sure I'm missing something.

    Can you take a look?

    Excuse the music... client insists on it!

    http://www.theinnatbowmanshill.com/NEW_SITE/

  6. #6
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    validate.js and menu.js are html documents ???

    The body onload must be removed.
    Add that function here:
    Code:
    <script type="text/javascript">
    window.onload=function() {
    MM_preloadImages('../images/1_on.jpg','../images/2_on.jpg','../images/3_on.jpg','../images/4_on.jpg','../images/5_on.jpg','../images/6_on.jpg');
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    var aObj=document.getElementsByTagName('a');
    var i=aObj.length; 
    while(i--) { 
        if(aObj[i].href.indexOf(sPage )!=-1) {aObj[i].className = 'current';}
        }
    };	
    </script>
    At least 98% of internet users' DNA is identical to that of chimpanzees

  7. #7
    Join Date
    Oct 2007
    Posts
    15

    Works!

    BTW, those js files are no longer needed in the new site so they're coming up page not found hence the html.

    Your script is working, thanks!

    Two questions...

    1) The style of the current link is changing after the link loads. Is it possible to get it to paint with the current style the first time?

    2) the Gallery page works with a get parameter so all the sublinks take the current style. Is there a way to account for the 'dir' parameter so only one link paints current?

    Thanks again!

  8. #8
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    1) Use the solution given in the link (css) or do this server-side. It may be possible to get it to show slightly quicker by running the script at the top of the jQuery onload.

    2)
    Code:
    window.onload=function() {
    MM_preloadImages('../images/1_on.jpg','../images/2_on.jpg','../images/3_on.jpg','../images/4_on.jpg','../images/5_on.jpg','../images/6_on.jpg');
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    var aObj=document.getElementsByTagName('a');
    for(var i=0; i<aObj.length; i++) { 
        if(aObj[i].href.indexOf(sPage )!=-1) {aObj[i].className = 'current'; break;}
        }
    };
    At least 98% of internet users' DNA is identical to that of chimpanzees

  9. #9
    Join Date
    Oct 2007
    Posts
    15

    thanks

    I'll try changing the load order.

    Thanks again. You saved me writing lots of clumsy php code!

  10. #10
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    You saved me writing lots of clumsy php code
    It's a lot easier doing this with php.
    At least 98% of internet users' DNA is identical to that of chimpanzees

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles