I have a menu where the selected item (whose href is equal to the current page) needs to look different to the other menu items. I can either:
-
Compare each menu item
hreftowindow.locationusing JavaScript and addclass="selected"to the right element on DOM load, or -
Serve only the selected menu item with
class="selected"by checking eachhrefagainst something likeRequest.ServerVariables("URL")server side.
This must be a common situation – which method is generally preferred? To me the second option is probably cleaner but the menu ASP page is less maintainable compared to the first option which is HTML only.
On the client side you’ll have a little time when there is no selected menu (it’ll increase if you load external script after you displayed your menu). If someone has an old computer with ie6 it can be really really slow.
Javascript is used to make animations, server request (limit those as much as it can with validations), here you are preparing your document so for me it’s a server responsibility.
I always try to limit the executed javascript on the “load” event, it there is to much stuff executing it can really decrease the user experience.