Thursday, May 2, 2024
 Popular · Latest · Hot · Upcoming
182
rated 0 times [  186] [ 4]  / answers: 1 / hits: 15558  / 15 Years ago, wed, june 3, 2009, 12:00:00

I have the following menu in my masterpage:



<ul id=menu class=lavaLampBottomStyle>
<li>
<%= Html.ActionLink(Employees, Index, Employees)%></li>
<li>
<%= Html.ActionLink(Customer, Details, Account)%></li>
</ul>


I need a way to set the css class of the current active li to current.



My first guess it to do this with the assistance of javascript.



I would include something like this in the masterpage:



  $(#menu li a).each(){
if($(this).attr(href) == '<%= *GET CURRENT PAGE* %>'){
$(this).parent(li).addClass(current);
}
}


Is this a good approach?



If it is, how can I get the current URL part like in the href?



If it isn't, what's your suggestion? :-)



FYI, the generated html I'm after:



<ul id=menu class=lavaLampBottomStyle>
<li>
<a href=/KszEmployees/Index>Employees</a></li>
<li>
<a class=current href=/>Customer</a></li>
</ul>

More From » asp.net-mvc

 Answers
62

That's probably the least intensive way of doing it. If you can count on the users to have javascript enabled, I see nothing wrong with this, and have done it myself on occasion.



Request.Url is the object you are interested in to get the current page on the server side. The suggestion to use window.location.href by tvanfosson isn't bad either if you want to keep it entirely clientside.



The advantage of using serverside, is that Request.Url has easily accessible parts of the url, such as Request.Url.Host, etc to help with your link-munging needs.


[#99399] Friday, May 29, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
matteo

Total Points: 81
Total Questions: 100
Total Answers: 96

Location: Honduras
Member since Sat, Jul 24, 2021
3 Years ago
matteo questions
Tue, Mar 8, 22, 00:00, 2 Years ago
Sun, May 31, 20, 00:00, 4 Years ago
Thu, Mar 12, 20, 00:00, 4 Years ago
Tue, Jan 22, 19, 00:00, 5 Years ago
Wed, Sep 12, 18, 00:00, 6 Years ago
;