Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
jQuery 'active page' problem
Old 04-05-2009, 08:32 AM jQuery 'active page' problem
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
I have my navigation coded like so
Code:
<ul id="nav">
        <li><a href="../contact.htm">contact us</a></li>
        <li><a href="../about.htm">about us</a></li>
        <li><a href="../myAccount/">my account</a></li>
        <li><a href="../register.htm">sign up</a></li>
        <li><a href="../search.htm">search</a></li>
        <li></li>
      </ul>
and Im using the following code to make jQuery change the class of the currently viewed page to my 'active' class that has been specified in the CSS
Code:
// make the current page show as active on the nav
$(document).ready (function () {
  var path = location.pathname.substring(1);
  $('#nav a[@href$="' + path + '"]').addClass('active');
});
But nothing happens! I check the Firefox error console (which doesn't even issue an alert for some reason) and I get a message similar to the following
Quote:
Error: [Exception... "'Syntax error, unrecognized expression: [@href$="C:/Users/Pealo/Documents/homework/levelH/researchDevelopment/penguinpages.co.uk/register.htm"]' when calling method: [nsIDOMEventListener::handleEvent]" nsresult: "0x8057001e (NS_ERROR_XPC_JS_THREW_STRING)" location: "<unknown>" data: no]
I'm clueless on how to fix it though! Any help would be great.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is online now
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
 
Register now for full access!
Old 04-05-2009, 11:03 AM Re: jQuery 'active page' problem
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Code:
$("#nav a").each(function() {
     if(this.href == window.location) $(this).addClass("active");
});
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 04-05-2009, 11:12 AM Re: jQuery 'active page' problem
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
thanks that works! Im guessing the 'each' method is used to cycle through each item in the list?

I was going by this tutorial:
http://jeroencoumans.nl/journal/simp...le-jquery-menu

But it didn't work in my case! No too sure why though
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is online now
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 04-05-2009, 11:24 AM Re: jQuery 'active page' problem
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Hmmm I've just thought, I don't know if this method would work in cases such as where the location is something like

"search.php?companyId=23"

Do you know of a way around this by any chance?
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is online now
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Old 04-06-2009, 09:59 AM Re: jQuery 'active page' problem
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Code:
$("#nav a").each(function() {
if(this.href == window.location || this.href == document.location.protocol + "//" + window.location.hostname + window.location.pathname)
     $(this).addClass("active");
});
or, you could use CSS attribute selectors. I think this will work:
Code:
var path = document.location.protocol + "//" + window.location.hostname + window.location.pathname;
$("#nav a[href=" +  window.location + "], #nav a[href=" + path + "]").addClass("active");
It is important to note that paths contained inside of any href property always manifest as absolute paths in JavaScript, no matter how they are expressed in the HTML.
__________________
Join me on
Please login or register to view this content. Registration is FREE

Last edited by wayfarer07; 04-14-2009 at 02:18 PM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 04-08-2009, 03:26 PM Re: jQuery 'active page' problem
pealo86's Avatar
Super Spam Talker

Posts: 850
Name: Matt Pealing
Location: England, north west
Trades: 0
Thanks!
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
pealo86 is online now
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Reply     « Reply to jQuery 'active page' problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.64468 seconds with 12 queries