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
href
towindow.location
using JavaScript and addclass="selected"
to the right element on DOM load, orServe only the selected menu item with
class="selected"
by checking eachhref
against 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.
Whenever possible, I try to handle that kind of thing server-side as much as possible. That way, it'll still function as expected for users with Javascript disabled, as well as avoiding the quick flash of unstyled content remi mentioned.
精彩评论