SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular Expressions...my nightmare

    Hi!

    I'm creating a personal website and I my navigation is in the sidebar. When I view a specific page I would like to add a class "Active" for that link so I would know what page I'm currently viewing.

    I've tried using this:

    var context = item.innerText || item.textContent;
    if (context && currentLocation.indexOf(context) >= 0) {
    return item.className = 'Active';
    }

    where item is a reference to each link in the sidebar.

    Works ok, but my problem is that I have two pages named like this:
    - viewElement.htm
    and
    - views.htm

    When I view the viewElement.htm both links are marked as "Active" ...

    I thought I could use regular expressions so I can test for a matching pattern(?)

    Something like this:
    When I view this page:
    url : http://localhost/folder/viewElement.htm
    the menu item (view element) from my sidebar to become active (and not the view menu item)

    Does anyone know how to do this?

    TIA for any help.
    Proud member of this community.
    ____________________________

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    viva W3C!!

    I have found my answer! I'll post my solution just in case someone else might face this problem:

    var context = (item.innerText || item.textContent) + ".htm";
    try {
    var pattern = new RegExp(context);
    if (pattern.test(currentLocation)) {
    item.className =
    'Active';
    }
    }
    catch(ex) {}


    All I had to do is to append the file extension. cool.
    Proud member of this community.
    ____________________________

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Why are you using innerText (which is IE only) rather than innerHTML which is supported by all major browsers.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no particular reason... (maybe it was just the first thing that came to my mind...) though you're right, innerHTML would be better.
    Proud member of this community.
    ____________________________

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Also, you'd be better off controlling this on the server side. Much neater.

  6. #6
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's true, indeed. But I think now it's kind of late for that, I already implemented this in almost 50&#37; of pages in my project and I'm not happy going through each of those pages again just to change the class that each page implements...uggghh


    Should I've had thought of that in the first place... (slaps head)
    Proud member of this community.
    ____________________________


Bookmarks

Posting Permissions

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